首页 > 代码库 > WMI和devenv写了个小软件

WMI和devenv写了个小软件

最近把公司用的电脑重装了一下,期间用到了驱动精灵,驱动精灵把电脑的全方面信息都显示出来了,让人有种一目了然的感觉,为什么我不自己也写个呢?虽然显示的数据不一定有驱动精灵全单至少是我自己写的,不是吗?

软件写好后,看着显示出来的电脑信息虽然不是很全但是基本信息都有,普通人就够了。这时我又想起了一件事 ,每天上班第一件事是开电脑,第二件事就是打开VS(我的是vs2012),既然每天都固定要做为什么不让它开机就启动呢?于是我结合每天打开vs2012的命令上网搜了些资料完成了此功能,以下是截图,虽然丑了点:

 

  1 #region 硬件信息
  2         //主板信息
  3         public void GetBaseBroad()
  4         {
  5             ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_BaseBoard");
  6             foreach (ManagementObject mo in mos.Get())
  7             {
  8                 lblBaseBroad.Text = mo["Product"].ToString();
  9                 lblBaseBoardProduct.Text = mo["Manufacturer"].ToString();
 10             }
 11 
 12         }
 13         //CPU信息
 14         public void GetCPU()
 15         {
 16             ManagementClass process = new ManagementClass("Win32_Processor");
 17             foreach (ManagementObject mo in process.GetInstances())
 18             {
 19                 //lblCPUTpye.Text = mo["processorId"].ToString();
 20                 lblCPUName.Text = mo["Name"].ToString();
 21                 lblSystem.Text = mo["CurrentClockSpeed"].ToString();
 22             }
 23         }
 24 
 25         //电脑型号
 26         public void GetComputer()
 27         {
 28             ManagementClass computer = new ManagementClass("Win32_OperatingSystem");
 29             foreach (ManagementObject mo in computer.GetInstances())
 30             {
 31                 lblSystem.Text = mo["Caption"].ToString() + "(" + mo["CSDVersion"].ToString() + ")";
 32                 lblComputerName.Text = mo["CSName"].ToString();
 33             }
 34         }
 35 
 36         //内存
 37         public void GetRAM()
 38         {
 39             ManagementClass ram = new ManagementClass("Win32_PhysicalMemory");
 40             foreach (ManagementObject mo in ram.GetInstances())
 41             {
 42                 lblRAM.Text = (Int64.Parse(mo["Capacity"].ToString()) / 1024 / 1024 / 1024).ToString() + "GB (" + mo["Manufacturer"].ToString() + GetDDR(int.Parse(mo["MemoryType"].ToString())) + ")";
 43             }
 44         }
 45 
 46         public string GetDDR(int ddr)
 47         {
 48             string res = string.Empty;
 49             switch (ddr)
 50             {
 51                 case 0: res = "Unknown"break;
 52                 case 1: res = "Other"break;
 53                 case 2: res = "DRAM"break;
 54                 case 3: res = "Synchronous DRAM"break;
 55                 case 4: res = "Cache DRAM"break;
 56                 case 5: res = "EDO"break;
 57                 case 6: res = "EDRAM"break;
 58                 case 7: res = "VRAM"break;
 59                 case 8: res = "SRAM"break;
 60                 case 9: res = "RAM"break;
 61                 case 10: res = "ROM"break;
 62                 case 11: res = "Flash"break;
 63                 case 12: res = "EEPROM"break;
 64                 case 13: res = "FEPROM"break;
 65                 case 14: res = "EPROM"break;
 66                 case 15: res = "CDRAM"break;
 67                 case 16: res = "3DRAM"break;
 68                 case 17: res = "SDRAM"break;
 69                 case 18: res = "SGRAM"break;
 70                 case 19: res = "RDRAM"break;
 71                 case 20: res = "DDR"break;
 72                 case 21: res = "DDR-2"break;
 73                 default: res = "Non"break;
 74             }
 75             return res;
 76         }
 77 
 78         //硬盘
 79         public void GetDisk()
 80         {
 81             ManagementClass disk = new ManagementClass("Win32_DiskDrive");
 82             foreach (ManagementObject mo in disk.GetInstances())
 83             {
 84                 lblDisk.Text = mo["Model"].ToString() + "---" + (Int64.Parse(mo["Size"].ToString()) / 1024 / 1024 / 1024).ToString() + " GB";
 85             }
 86         }
 87 
 88         //网卡
 89         public void GetNetWorkCard()
 90         {
 91             ManagementClass disk = new ManagementClass("Win32_NetworkAdapter");
 92             foreach (ManagementObject mo in disk.GetInstances())
 93             {
 94                 lblNetWorkCard.Text = mo["Description"].ToString() + "------";
 95             }
 96         }
 97 
 98         //显卡
 99         public void GetDisplay()
100         {
101             ManagementClass disk = new ManagementClass("Win32_DisplayConfiguration");
102             foreach (ManagementObject mo in disk.GetInstances())
103             {
104                 lblDisplay.Text = mo["Caption"].ToString() + "------" + mo["BitsPerPel"].ToString();
105             }
106         }
107         //显示器
108         public void GetDisplayMonitor()
109         {
110             ManagementClass disk = new ManagementClass("Win32_DesktopMonitor");
111             foreach (ManagementObject mo in disk.GetInstances())
112             {
113                 lblDisplayMonitor.Text = mo["MonitorManufacturer"].ToString() + "----" + mo["PNPDeviceID"].ToString();
114             }
115         }
116         //声卡
117         public void GetSound()
118         {
119             ManagementClass disk = new ManagementClass("Win32_SoundDevice");
120             foreach (ManagementObject mo in disk.GetInstances())
121             {
122                 //lblSound.Text = mo["Manufacturer"].ToString() + "---" + mo["MPU401Address"].ToString();
123             }
124         }
125 

126         #endregion

  1 #region 系统操作

 2         private void btnReboot_Click(object sender, EventArgs e)
 3         {
 4             OperatingSystemControl("重启""Reboot"new object[] { ComputerStatus.Reboot });
 5         }
 6 
 7         private void btnShutDown_Click(object sender, EventArgs e)
 8         {
 9             OperatingSystemControl("关机""ShutDown"new object[] { ComputerStatus.Shutdown });
10         }
11 
12 
13         public void OperatingSystemControl(string word, string cmd, object[] prams)
14         {
15             DialogResult result = MessageBox.Show("确定要" + word + "吗?""请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
16             if (result == DialogResult.Yes)
17             {
18                 ManagementClass mo = new ManagementClass("Win32_OperatingSystem");
19                 mo.Scope.Options.EnablePrivileges = true;
20                 foreach (ManagementObject n in mo.GetInstances())
21                 {
22                     n.InvokeMethod(cmd, prams);
23                 }
24                 mo.Dispose();
25             }
26         }
27 
28         private void btnLogoff_Click(object sender, EventArgs e)
29         {
30             OperatingSystemControl("注销""Win32Shutdown"new object[] { ComputerStatus.LogOff });
31         }
32 
33         private void btnForcedLogoff_Click(object sender, EventArgs e)
34         {
35             OperatingSystemControl("强制注销""Win32Shutdown"new object[] { ComputerStatus.ForcedLogOff });
36         }
37 
38         private void btnForcedReboot_Click(object sender, EventArgs e)
39         {
40             OperatingSystemControl("强制重启""Win32Shutdown"new object[] { ComputerStatus.ForcedReboot });
41         }
42 
43         private void btnForcedShutDown_Click(object sender, EventArgs e)
44         {
45             OperatingSystemControl("强制关机""Win32Shutdown"new object[] { ComputerStatus.ForcedShutdown });
46         }
47 
48         private void btnPoweroff_Click(object sender, EventArgs e)
49         {
50             OperatingSystemControl("关闭电源""Win32Shutdown"new object[] { ComputerStatus.PowerOff });
51         }
52 
53         private void btnFocedPoweroff_Click(object sender, EventArgs e)
54         {
55             OperatingSystemControl("强制关闭电源""Win32Shutdown"new object[] { ComputerStatus.ForcedPowerOff });
56         }
57         #endregion
 1 #region VS操作
 2         private void btnStartiPush_Click(object sender, EventArgs e)
 3         {
 4             //Process p = new Process();
 5             //p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe";
 6             //p.StartInfo.Arguments = string.Format("/command File.OpenFile {0}", @"E:\Work\iPush.sln");    //設定程式執行參數
 7             //p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
 8             //p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
 9             //p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
10             //p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
11             //p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
12 
13             //p.Start();   //啟動
14 
15             StartProject(cbProjects.SelectedValue.ToString());
16         }
17         public void StartProject(string projectUrl)
18         {
19             Process p = new Process();
20             p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe";
21             p.StartInfo.Arguments = string.Format("{0}", projectUrl);    //設定程式執行參數
22             p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
23             p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
24             p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
25             p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
26             p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
27 
28             p.Start();   //啟動
29         }
30         private void btnChoiseFile_Click(object sender, EventArgs e)
31         {
32             OpenFileDialog file = new OpenFileDialog();
33             if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
34             {
35                 string fileUrl = file.FileName;
36                 string type = fileUrl.Substring(fileUrl.LastIndexOf(.));
37                 if (type == ".sln")
38                 {
39                     MessageBox.Show(file.SafeFileName);
40                     //StartProject(fileUrl);
41                     XmlDocument doc = new XmlDocument();
42                     doc.Load("Project.xml");
43                     XmlNode node = doc.SelectSingleNode("projects");
44                     XmlElement xe1 = doc.CreateElement("project");
45                     xe1.SetAttribute("id", file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf(.)));
46                     //xe1.SetAttribute("name", file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf(‘.‘)));
47                     //xe1.SetAttribute("path", fileUrl);
48                     XmlElement xesub1 = doc.CreateElement("name");
49                     xesub1.InnerText = file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf(.));
50                     xe1.AppendChild(xesub1);
51                     XmlElement xesub2 = doc.CreateElement("path");
52                     xesub2.InnerText = fileUrl;
53                     xe1.AppendChild(xesub2);
54                     node.AppendChild(xe1);
55                     doc.Save("Project.xml");
56 
57                     initProjects();
58                 }
59             }
60             else
61             {
62                 MessageBox.Show("false");
63             }
64         }
65 

66         #endregion 

以上是主要代码,代码简单请不要见笑。