iT邦幫忙

DAY 3
7

c#應用系列 第 3

C#應用(3)以WMI寫個SysInfo小工具

  • 分享至 

  • xImage
  •  

WMI取得一些電腦內的系統資料

相關技術:
*WMI
*WMI Code Creator v1.0(用來查各WMI裡有那些可用資料)
*TabControl Class
*ListView Class

回目錄
1.先新增一個C#的Windows Form應用程式的專案, 名稱為 SysInfo
2.加入參考System.Management.dll
3.在Form1上用"工具箱"拉一個TabControl上去, 拉大到整個Form1的Size
4.在tabControl1加入事件SelectedIndexChanged
在Form1加入事件SizeChanged

*檔案 Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace SysInfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Text = "Sysinfo";
            this.tabControl1.TabPages.Clear();

            string [] TabName = {
                                    "OperatingSystem",
                                    "Processor",
                                    "LogicalDisk",
                                    "Service",
                                    "Process"
                                };
            int i = 0;
            foreach (string tabName in TabName) 
            {
                this.tabControl1.TabPages.Add(tabName);
                this.tabControl1.TabPages[i].Show();
                i++;
            }

            tabControl1_SelectedIndexChanged(null, null);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TabPage tab = this.tabControl1.TabPages[this.tabControl1.SelectedIndex];
            switch (tab.Text)
            {
                case "OperatingSystem":
                    Info.OperatingSystem.Show(tab);
                    break;
                case "Processor":
                    Info.Processor.Show(tab);
                    break;
                case "LogicalDisk":
                    Info.LogicalDisk.Show(tab);
                    break;
                case "Service": 
                    Info.Service.Show(tab); 
                    break;
                case "Process":
                    Info.Process.Show(tab);
                    break;
                default:
                    break;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            tabControl1.Width = this.FindForm().Width;
            tabControl1.Height = this.FindForm().Height - SystemInformation.CaptionHeight;

            TabPage tab = this.tabControl1.TabPages[this.tabControl1.SelectedIndex];
            if (tab.Controls.Count > 0)
            {
                ListView listView1;
                if (tab.Controls.Find("listView1", false).Length > 0)
                {
                    listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                    listView1.BeginUpdate();
                    listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
                    listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                        - SystemInformation.CaptionHeight
                        - SystemInformation.ToolWindowCaptionButtonSize.Height;
                    listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    listView1.EndUpdate();
                }
            }
        }
    }
}

5.新增資料夾 Info 然後再此資料夾內,新增類別, 分別是
OperatingSystem
Processor
LogicalDisk
Service
Process

*檔案 OperatingSystem.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace SysInfo.Info
{
    class OperatingSystem
    {
        public static void Show(TabPage tab)
        {
            string[] ColumnName = {
                                      "OS Name",
                                      "Version",
                                      "BuildNumber",
                                      "CodeSet",
                                      "SerialNumber",
                                      "ServicePackMajorVersion",
                                      "ServicePackMinorVersion",
                                      "MaxProcessMemorySize",
                                      "SystemDirectory",
                                      "Instance Path"
                                  };
            string[] ItemName = { 
                                    "Version",
                                    "BuildNumber",
                                    "CodeSet",
                                    "SerialNumber",
                                    "ServicePackMajorVersion",
                                    "ServicePackMinorVersion",
                                    "MaxProcessMemorySize",
                                    "SystemDirectory"
                                };

            ManagementClass c = new ManagementClass("Win32_OperatingSystem");

            ListView listView1;
            if (tab.Controls.Find("listView1", false).Length > 0)
            {
                listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                listView1.Items.Clear();
            }
            else
            {
                listView1 = new ListView();
                listView1.Name = "listView1";
                listView1.View = View.Details;
                listView1.Scrollable = true;
                listView1.FullRowSelect = true;

                foreach (string columnName in ColumnName)
                {
                    listView1.Columns.Add(columnName);
                }

                tab.Controls.Add(listView1);
                listView1.Show();
            }

            listView1.BeginUpdate();
            listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
            listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                - SystemInformation.CaptionHeight
                - SystemInformation.ToolWindowCaptionButtonSize.Height;
            foreach (ManagementObject o in c.GetInstances())
            {
                ListViewItem item = new ListViewItem();
                item.Text = o["Caption"].ToString();
                foreach (string itemName in ItemName)
                {
                    item.SubItems.Add(o[itemName].ToString());
                }
                item.SubItems.Add(o.Path.ToString());

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();
        }
    }
}

*檔案 Processor.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace SysInfo.Info
{
    class Processor
    {
        public static void Show(TabPage tab)
        {
            string[] ColumnName = {
                                      "DeviceID",
                                      "Name",
                                      "Description",
                                      "ProcessorId",
                                      "NumberOfCores",
                                      "MaxClockSpeed",
                                      "SocketDesignation",
                                      "L2CacheSize",
                                      "Instance Path"
                                  };
            string[] ItemName = { 
                                    "Name",
                                    "Description",
                                    "ProcessorId",
                                    "NumberOfCores",
                                    "MaxClockSpeed",
                                    "SocketDesignation",
                                    "L2CacheSize"
                                };

            ManagementClass c = new ManagementClass("Win32_Processor");

            ListView listView1;
            if (tab.Controls.Find("listView1", false).Length > 0)
            {
                listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                listView1.Items.Clear();
            }
            else
            {
                listView1 = new ListView();
                listView1.Name = "listView1";
                listView1.View = View.Details;
                listView1.Scrollable = true;
                listView1.FullRowSelect = true;

                foreach (string columnName in ColumnName)
                {
                    listView1.Columns.Add(columnName);
                }

                tab.Controls.Add(listView1);
                listView1.Show();
            }

            listView1.BeginUpdate();
            listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
            listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                - SystemInformation.CaptionHeight
                - SystemInformation.ToolWindowCaptionButtonSize.Height;
            foreach (ManagementObject o in c.GetInstances())
            {
                ListViewItem item = new ListViewItem();
                item.Text = o["DeviceID"].ToString();
                foreach (string itemName in ItemName)
                {
                    item.SubItems.Add(o[itemName].ToString());
                }
                item.SubItems.Add(o.Path.ToString());

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();
        }
    }
}

*檔案 LogicalDisk.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace SysInfo.Info
{
    class LogicalDisk
    {
        public static void Show(TabPage tab)
        {
            string[] ColumnName = {
                                      "Disk Name",
                                      "Description",
                                      "Size",
                                      "FreeSpace",
                                      "FileSystem",
                                      "Compressed",
                                      "Instance Path"
                                  };
            string[] ItemName = { 
                                    "Description",
                                    "Size",
                                    "FreeSpace",
                                    "FileSystem",
                                    "Compressed",
                                };

            ManagementClass c = new ManagementClass("Win32_LogicalDisk");

            ListView listView1;
            if (tab.Controls.Find("listView1", false).Length > 0)
            {
                listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                listView1.Items.Clear();
            }
            else
            {
                listView1 = new ListView();
                listView1.Name = "listView1";
                listView1.View = View.Details;
                listView1.Scrollable = true;
                listView1.FullRowSelect = true;

                foreach (string columnName in ColumnName)
                {
                    listView1.Columns.Add(columnName);
                }

                tab.Controls.Add(listView1);
                listView1.Show();
            }

            listView1.BeginUpdate();
            listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
            listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                - SystemInformation.CaptionHeight
                - SystemInformation.ToolWindowCaptionButtonSize.Height;
            foreach (ManagementObject o in c.GetInstances())
            {
                ListViewItem item = new ListViewItem();
                item.Text = o["Name"].ToString();
                foreach (string itemName in ItemName)
                {
                    item.SubItems.Add(o[itemName].ToString());
                }
                item.SubItems.Add(o.Path.ToString());

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();
        }
    }
}

*檔案 Service.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace SysInfo.Info
{
    class Service
    {
        public static void Show(TabPage tab)
        {
            string[] ColumnName = {
                                      "Service Name",
                                      "DisplayName",
                                      "Description",
                                      "ServiceType",
                                      "Started",
                                      "StartMode",
                                      "StartName",
                                      "State",
                                      "PathName",
                                      "ProcessId",
                                      "Instance Path"
                                  };
            string[] ItemName = { 
                                    "DisplayName",
                                    "Description",
                                    "ServiceType",
                                    "Started",
                                    "StartMode",
                                    "StartName",
                                    "State",
                                    "PathName",
                                    "ProcessId"
                                };

            ManagementClass c = new ManagementClass("Win32_Service");

            ListView listView1;
            if (tab.Controls.Find("listView1", false).Length > 0)
            {
                listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                listView1.Items.Clear();
            }
            else 
            {
                listView1 = new ListView();
                listView1.Name = "listView1";
                listView1.View = View.Details;
                listView1.Scrollable = true;
                listView1.FullRowSelect = true;

                foreach (string columnName in ColumnName)
                {
                    listView1.Columns.Add(columnName);
                }

                tab.Controls.Add(listView1);
                listView1.Show();                
            }

            listView1.BeginUpdate();
            listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
            listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                - SystemInformation.CaptionHeight
                - SystemInformation.ToolWindowCaptionButtonSize.Height;
            foreach (ManagementObject o in c.GetInstances())
            {
                ListViewItem item = new ListViewItem();
                item.Text =  o["Name"].ToString();
                foreach (string itemName in ItemName) {
                    item.SubItems.Add(o[itemName].ToString());
                }
                item.SubItems.Add(o.Path.ToString());

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();
        }
    }
}

*檔案 Process.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace SysInfo.Info
{
    class Process
    {
        public static void Show(TabPage tab)
        {
            string[] ColumnName = {
                                    "Process Name",
                                    "CommandLine",
                                    "ProcessId",
                                    "Owner",
                                    "Instance Path"
                                };
            string[] ItemName = { 
                                "CommandLine",
                                "ProcessId"
                            };

            ManagementClass c = new ManagementClass("Win32_Process");

            ListView listView1;
            if (tab.Controls.Find("listView1", false).Length > 0)
            {
                listView1 = tab.Controls.Find("listView1", false)[0] as ListView;
                listView1.Items.Clear();
            }
            else
            {
                listView1 = new ListView();
                listView1.Name = "listView1";
                listView1.View = View.Details;
                listView1.Scrollable = true;
                listView1.FullRowSelect = true;

                foreach (string columnName in ColumnName)
                {
                    listView1.Columns.Add(columnName);
                }

                tab.Controls.Add(listView1);
                listView1.Show();
            }

            listView1.BeginUpdate();
            listView1.Width = tab.FindForm().Width - SystemInformation.VerticalScrollBarWidth;
            listView1.Height = tab.FindForm().Height - SystemInformation.HorizontalScrollBarHeight
                - SystemInformation.CaptionHeight
                - SystemInformation.ToolWindowCaptionButtonSize.Height;
            foreach (ManagementObject o in c.GetInstances())
            {
                ListViewItem item = new ListViewItem();
                item.Text = o["Name"].ToString();
                foreach (string itemName in ItemName)
                {
                    if (o[itemName] != null)
                        item.SubItems.Add(o[itemName].ToString());
                    else
                        item.SubItems.Add("");
                }

                ManagementBaseObject outParams = o.InvokeMethod("GetOwner", null, null);
                if ((outParams["Domain"] != null) && (outParams["User"] != null))
                    item.SubItems.Add(outParams["Domain"].ToString() + "/" + outParams["User"].ToString());
                else
                    item.SubItems.Add("");

                item.SubItems.Add(o.Path.ToString());

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();
        }
    }
}

6.最後, 建置 SysInfo, 就這樣了
因為沒時間慢慢寫, 所以只先做雛型, 對於此SysInfo功能有興趣的人
後續功能請自己加


上一篇
C#應用(2)取得台灣天氣資料
下一篇
C#應用(4)jqGrid的條件式搜尋
系列文
c#應用13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言