WMI查看电脑信息,devenv管理自己的解决方案

时间:2021-07-27 23:32:29

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

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

WMI查看电脑信息,devenv管理自己的解决方案WMI查看电脑信息,devenv管理自己的解决方案WMI查看电脑信息,devenv管理自己的解决方案WMI查看电脑信息,devenv管理自己的解决方案 

  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()
         {
             ManagementClass disk = new ManagementClass("Win32_DisplayConfiguration");
             foreach (ManagementObject mo in disk.GetInstances())
             {
                 lblDisplay.Text = mo["Caption"].ToString() + "------" + mo["BitsPerPel"].ToString();
             }
         }
         //显示器
         public void GetDisplayMonitor()
         {
             ManagementClass disk = new ManagementClass("Win32_DesktopMonitor");
             foreach (ManagementObject mo in disk.GetInstances())
             {
                 lblDisplayMonitor.Text = mo["MonitorManufacturer"].ToString() + "----" + mo["PNPDeviceID"].ToString();
             }
         }
         //声卡
         public void GetSound()
         {
             ManagementClass disk = new ManagementClass("Win32_SoundDevice");
             foreach (ManagementObject mo in disk.GetInstances())
             {
                 //lblSound.Text = mo["Manufacturer"].ToString() + "---" + mo["MPU401Address"].ToString();
             }
         }
 

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 });
         }
 
 
         public void OperatingSystemControl(string word, string cmd, object[] prams)
         {
             DialogResult result = MessageBox.Show("确定要" + word + "吗?", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (result == DialogResult.Yes)
             {
                 ManagementClass mo = new ManagementClass("Win32_OperatingSystem");
                 mo.Scope.Options.EnablePrivileges = true;
                 foreach (ManagementObject n in mo.GetInstances())
                 {
                     n.InvokeMethod(cmd, prams);
                 }
                 mo.Dispose();
             }
         }
 
         private void btnLogoff_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("注销", "Win32Shutdown", new object[] { ComputerStatus.LogOff });
         }
 
         private void btnForcedLogoff_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("强制注销", "Win32Shutdown", new object[] { ComputerStatus.ForcedLogOff });
         }
 
         private void btnForcedReboot_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("强制重启", "Win32Shutdown", new object[] { ComputerStatus.ForcedReboot });
         }
 
         private void btnForcedShutDown_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("强制关机", "Win32Shutdown", new object[] { ComputerStatus.ForcedShutdown });
         }
 
         private void btnPoweroff_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("关闭电源", "Win32Shutdown", new object[] { ComputerStatus.PowerOff });
         }
 
         private void btnFocedPoweroff_Click(object sender, EventArgs e)
         {
             OperatingSystemControl("强制关闭电源", "Win32Shutdown", new object[] { ComputerStatus.ForcedPowerOff });
         }
         #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;  //重定向標準輸出
             //p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
             //p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
 
             //p.Start();   //啟動
 
             StartProject(cbProjects.SelectedValue.ToString());
         }
         public void StartProject(string projectUrl)
         {
             Process p = new Process();
             p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe";
             p.StartInfo.Arguments = string.Format("{0}", projectUrl);    //設定程式執行參數
             p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
             p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
             p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
             p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
             p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
 
             p.Start();   //啟動
         }
         private void btnChoiseFile_Click(object sender, EventArgs e)
         {
             OpenFileDialog file = new OpenFileDialog();
             if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 string fileUrl = file.FileName;
                 string type = fileUrl.Substring(fileUrl.LastIndexOf('.'));
                 if (type == ".sln")
                 {
                     MessageBox.Show(file.SafeFileName);
                     //StartProject(fileUrl);
                     XmlDocument doc = new XmlDocument();
                     doc.Load("Project.xml");
                     XmlNode node = doc.SelectSingleNode("projects");
                     XmlElement xe1 = doc.CreateElement("project");
                     xe1.SetAttribute("id", file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf('.')));
                     //xe1.SetAttribute("name", file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf('.')));
                     //xe1.SetAttribute("path", fileUrl);
                     XmlElement xesub1 = doc.CreateElement("name");
                     xesub1.InnerText = file.SafeFileName.Substring(0, file.SafeFileName.LastIndexOf('.'));
                     xe1.AppendChild(xesub1);
                     XmlElement xesub2 = doc.CreateElement("path");
                     xesub2.InnerText = fileUrl;
                     xe1.AppendChild(xesub2);
                     node.AppendChild(xe1);
                     doc.Save("Project.xml");
 
                     initProjects();
                 }
             }
             else
             {
                 MessageBox.Show("false");
             }
         }
 66         #endregion 

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