CQ-CSER

计算机爱好者

c#调用系统各参数

Posted on | 三月 22, 2010 | 1 Comment

private void button1_Click(object sender, EventArgs e)//Environment类各参数
{//获取系统开机后经过的时间
string MyInfo = “当前系统开机后经过的时间是:”;
MyInfo += (Environment.TickCount/1000).ToString()+”秒。”;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button2_Click(object sender, EventArgs e)
{//获取计算机的系统目录
string MyInfo = “当前计算机的系统目录是:”;
MyInfo += Environment.SystemDirectory.ToString() ;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button3_Click(object sender, EventArgs e)
{//获取系统的所有逻辑驱动器
string MyInfo = “当前计算机的逻辑驱动器如下:”;
foreach (String MyDrive in Environment.GetLogicalDrives())
MyInfo += “\n” + MyDrive;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button4_Click(object sender, EventArgs e)
{//获取“启动”程序组目录全路径
string MyInfo = “用户“启动”程序组的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.Startup);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button5_Click(object sender, EventArgs e)
{//获取文档模板目录全路径
string MyInfo = “文档模板的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.Templates);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button6_Click(object sender, EventArgs e)
{//获取开始菜单目录全路径
string MyInfo = “开始菜单的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button7_Click(object sender, EventArgs e)
{//获取用户程序组目录全路径
string MyInfo = “用户程序组的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.Programs);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button8_Click(object sender, EventArgs e)
{//获取“我的图片”目录全路径
string MyInfo = ““我的图片”的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button9_Click(object sender, EventArgs e)
{//获取“我的音乐”目录全路径
string MyInfo = ““我的音乐”的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button10_Click(object sender, EventArgs e)
{//获取Internet临时文件目录全路径
string MyInfo = “Internet 临时文件的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button11_Click(object sender, EventArgs e)
{//获取Internet历史记录目录全路径
string MyInfo = “Internet 历史记录项的公共储存库的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.History);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button12_Click(object sender, EventArgs e)
{//获取收藏夹目录全路径
string MyInfo = “收藏夹的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button13_Click(object sender, EventArgs e)
{//获取桌面目录全路径
string MyInfo = “桌面的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button14_Click(object sender, EventArgs e)
{//获取共享组件目录全路径
string MyInfo = “共享组件的目录是:”;
MyInfo += Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button1_Click(object sender, EventArgs e)
{//启动控制台应用程序
System.Diagnostics.Process.Start(“cmd.exe”);
}

private void button2_Click(object sender, EventArgs e)
{//获取驱动器的文件系统名称
string MyDriveLetter = “C:\\”;
try
{
DriveInfo MyDriveInfo = new DriveInfo(MyDriveLetter);
string MyInfo = MyDriveInfo + ” 驱动器的文件系统是:” + MyDriveInfo.DriveFormat;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void button3_Click(object sender, EventArgs e)
{//获取驱动器的类型名称
string MyDriveLetter = “F:\\”;
try
{
DriveInfo MyDriveInfo = new DriveInfo(MyDriveLetter);
string MyInfo = MyDriveInfo + ” 驱动器” ;
switch (MyDriveInfo.DriveType)
{
case DriveType.CDRom:
MyInfo += “是一个光盘设备,如 CD 或 DVD-ROM。 “;
break;
case DriveType.Fixed :
MyInfo += “是一个固定磁盘。”;
break;
case DriveType.Network :
MyInfo += “是一个网络驱动器。”;
break;
case DriveType.NoRootDirectory :
MyInfo += “没有根目录。”;
break;
case DriveType.Ram :
MyInfo += “是一个 RAM 磁盘。”;
break;
case DriveType.Removable :
MyInfo += “是一个可移动存储设备,如软盘驱动器或 USB 闪存驱动器。”;
break;
case DriveType.Unknown :
MyInfo += “类型未知。”;
break;
}
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

private void button4_Click(object sender, EventArgs e)
{//获取驱动器的卷标信息
string MyDriveLetter = “C:\\”;
try
{
DriveInfo MyDriveInfo = new DriveInfo(MyDriveLetter);
string MyInfo = MyDriveInfo + ” 驱动器的卷标是:” + MyDriveInfo.VolumeLabel;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void button5_Click(object sender, EventArgs e)
{//获取驱动器的存储空间大小
string MyDriveLetter = “C:\\”;
try
{
DriveInfo MyDriveInfo = new DriveInfo(MyDriveLetter);
string MyInfo = MyDriveInfo + ” 驱动器:”;
MyInfo += “\n存储空间的总大小是:” + MyDriveInfo.TotalSize / 1048576 + “M”;
MyInfo += “\n可用空闲空间总量是:” + MyDriveInfo.TotalFreeSpace / 1048576 + “M”;
//MyInfo += “\n可用空闲空间量是:” + MyDriveInfo.AvailableFreeSpace / 1048576 + “M”;
MyInfo += “\n已经使用的空间量是:” + ((MyDriveInfo.TotalSize / 1048576)-(MyDriveInfo.TotalFreeSpace / 1048576)) + “M”;
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void button6_Click(object sender, EventArgs e)
{//获取当前计算机的内存信息
try
{
string MyInfo = “当前计算机的内存信息如下:”;
Microsoft.VisualBasic.Devices.Computer My = new Microsoft.VisualBasic.Devices.Computer();
MyInfo+=”\n物理内存总量(M):”+ (My.Info.TotalPhysicalMemory/1024/1024).ToString();
MyInfo+=”\n虚拟内存总量(M):”+(My.Info.TotalVirtualMemory/1024/1024).ToString();
MyInfo+=”\n可用物理内存总量(M):”+(My.Info.AvailablePhysicalMemory/1024/1024).ToString();
MyInfo+=”\n可用虚拟内存总量(M):”+(My.Info.AvailableVirtualMemory/1024/1024).ToString();
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.Message, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void button7_Click(object sender, EventArgs e)
{//获取计算机的显示设备信息
string MyInfo = “当前系统显示设备信息如下:”;
Screen[] MyScreens = Screen.AllScreens;
int MyBound = MyScreens.GetUpperBound(0);
for (int i = 0; i <= MyBound; i++)
{
MyInfo+=”\n显示边界: ” + MyScreens[i].Bounds.ToString();
MyInfo+=”\n显示器工作区: ” + MyScreens[i].WorkingArea.ToString();
MyInfo+=”\n是否是主显示器: ” + MyScreens[i].Primary.ToString();
MyInfo+= “\n显示设备名称: ” + MyScreens[i].DeviceName;
}
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button8_Click(object sender, EventArgs e)
{//获取Windows系统安装的区域性
CultureInfo[] MyCultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures);
string MyInfo = “当前Windows系统已经安装的区域性包括:”;
foreach (CultureInfo MyCulture in MyCultures)
{
MyInfo += MyCulture.Name + “、”;
}
MessageBox.Show(MyInfo, “信息提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button9_Click(object sender, EventArgs e)
{//启动操作系统日志管理器
System.Diagnostics.Process.Start(“eventvwr.msc”);
}

private void button10_Click(object sender, EventArgs e)
{//启动操作系统性能计数器
System.Diagnostics.Process.Start(“perfmon.msc”);
}

最后来个关机的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct LUID_AND_ATTRIBUTES
{
public LUID pLuid;
public int Attributes;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct LUID
{
public int LowPart;
public int HighPart;
}
public class PrivilegeException : Exception
{
public PrivilegeException() : base() { }
public PrivilegeException(string message) : base(message) { }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("advapi32.dll", EntryPoint = "OpenProcessToken", CharSet = CharSet.Ansi)]
private static extern int OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
private const int TOKEN_ADJUST_PRIVILEGES = 0×20;
private const int TOKEN_QUERY = 0×8;
[DllImport("user32.dll", EntryPoint = "FormatMessageA", CharSet = CharSet.Ansi)]
private static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, int Arguments);
private const int FORMAT_MESSAGE_FROM_SYSTEM = 0×1000;
protected static string FormatError(int number)
{
StringBuilder buffer = new StringBuilder(255);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, buffer, buffer.Capacity, 0);
return buffer.ToString();
}
[DllImport("advapi32.dll", EntryPoint = "LookupPrivilegeValueA", CharSet = CharSet.Ansi)]
private static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
private const int SE_PRIVILEGE_ENABLED = 0×2;
[DllImport("advapi32.dll", EntryPoint = "AdjustTokenPrivileges", CharSet = CharSet.Ansi)]
private static extern int AdjustTokenPrivileges(IntPtr TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
private void button1_Click(object sender, EventArgs e)
{//关闭计算机并关闭电源
if (MessageBox.Show(“是否现在关闭计算机及电源?”, “信息提示”, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
string privilege = “SeShutdownPrivilege”;
IntPtr tokenHandle = IntPtr.Zero;
LUID privilegeLUID = new LUID();
TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES tokenPrivileges;
if (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
if (LookupPrivilegeValue(“”, privilege, ref privilegeLUID) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
tokenPrivileges.Privileges.pLuid = privilegeLUID;
int size = 4;
if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
ExitWindowsEx(8, 0);
}
}
}
}

没下文的话就是成功了

相关文章:

  1. 读写WIN.INI配置文件:c#
  2. 调用rundll32等实现各种功能:c#
  3. wmi查询管理:c#
  4. VISUAL C#系列
  5. 启动控制面板各选项:c#

评论|Comments

One Response to “c#调用系统各参数”

  1. cq
    三月 22nd, 2010 @ 18:48

    很强大,完全不是shutdowm -a之流能比。再来个重启的
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using System.Runtime.InteropServices;
    using System.Diagnostics;

    namespace WindowsApplication1
    {
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct LUID_AND_ATTRIBUTES
    {
    public LUID pLuid;
    public int Attributes;
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct TOKEN_PRIVILEGES
    {
    public int PrivilegeCount;
    public LUID_AND_ATTRIBUTES Privileges;
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct LUID
    {
    public int LowPart;
    public int HighPart;
    }
    public class PrivilegeException : Exception
    {
    public PrivilegeException() : base() { }
    public PrivilegeException(string message) : base(message) { }
    }
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    [DllImport("advapi32.dll", EntryPoint = "OpenProcessToken", CharSet = CharSet.Ansi)]
    private static extern int OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
    private const int TOKEN_ADJUST_PRIVILEGES = 0×20;
    private const int TOKEN_QUERY = 0×8;
    [DllImport("user32.dll", EntryPoint = "FormatMessageA", CharSet = CharSet.Ansi)]
    private static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, int Arguments);
    private const int FORMAT_MESSAGE_FROM_SYSTEM = 0×1000;
    protected static string FormatError(int number)
    {
    StringBuilder buffer = new StringBuilder(255);
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, buffer, buffer.Capacity, 0);
    return buffer.ToString();
    }
    [DllImport("advapi32.dll", EntryPoint = "LookupPrivilegeValueA", CharSet = CharSet.Ansi)]
    private static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
    private const int SE_PRIVILEGE_ENABLED = 0×2;
    [DllImport("advapi32.dll", EntryPoint = "AdjustTokenPrivileges", CharSet = CharSet.Ansi)]
    private static extern int AdjustTokenPrivileges(IntPtr TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
    [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
    private static extern int ExitWindowsEx(int uFlags, int dwReserved);
    private void button1_Click(object sender, EventArgs e)
    {//重新启动计算机
    if (MessageBox.Show(“是否现在重新启动计算机?”, “信息提示”, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    == DialogResult.Yes)
    {
    string privilege = “SeShutdownPrivilege”;
    IntPtr tokenHandle = IntPtr.Zero;
    LUID privilegeLUID = new LUID();
    TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
    TOKEN_PRIVILEGES tokenPrivileges;
    if (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    if (LookupPrivilegeValue(“”, privilege, ref privilegeLUID) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    tokenPrivileges.PrivilegeCount = 1;
    tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
    tokenPrivileges.Privileges.pLuid = privilegeLUID;
    int size = 4;
    if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    ExitWindowsEx(2, 0);
    }
    }
    }
    }

留言|Leave a Reply





  • Archives

  • SUNSHINE

  • About

    本博客采用创作共用版权协议,要求署名、非商业用途和保持一致. 转载本博客内容也遵循“署名-非商业用途-保持一致”的创作共用协议.

    订阅

    Search

    Admin