C# - Dll Viewer 檢視程式執行所需組件
在 PTT 的 C# 板上,看到有人發問如何偵測某個程式,是否正在執行 ?
答案就是,使用 System.Diagnostics 命名空間的 Process 類別,就可以使用 GetProcessesByName 方法取得程式。
這讓我想到以前我在想的問題:是不是有程式幫助我們列出,某個程式所使用到的組件詳細資料,也就是 exe 與 dll 資料?
沒有想到,現在利用 .NET 2.0 就可以輕鬆寫出一個這樣的程式。除了上述的 Process 類別,再加上 ProcessModule 與 ProcessModuleCollection 即可。
程式執行畫面:
(1) 程式開啟後,就會載入目前正在執行的程式,按左上角的【更新】按鈕,即可重新整理。
(2) 點擊左方畫面中的程式,就會列出該程式執行所需要的組件。
(3) 點擊組件名稱,就會跳出組件的詳細資料。
程式碼:
1: using System;
2: using System.Collections.Generic;
3: using System.Diagnostics;
4: using System.Windows.Forms;
5:
6: namespace DllViewer
7: {
8: public class Form1 : Form
9: {
10: private string pName;
11: private string dllName;
12: private Process[] p;
13: private readonly List<string> ls = new List<string>();
14:
15: public Form1()
16: {
17: InitializeComponent();
18: }
19:
20: private void listAllProcess()
21: {
22: ls.Clear();
23: listBox1.Items.Clear();
24:
25: p = Process.GetProcesses();
26: foreach (Process pValue in p)
27: {
28: ls.Add(pValue.ProcessName);
29: }
30: ls.Sort();
31:
32: foreach (string s in ls)
33: {
34: listBox1.Items.Add(s);
35: }
36: }
37:
38: private void Form1_Load(object sender, EventArgs e)
39: {
40: toolTip1.SetToolTip(button1, "更新");
41: Properties.Settings ps = Properties.Settings.Default;
42: Size = ps.Size;
43: Location = ps.Location;
44: listAllProcess();
45: }
46:
47: private void button1_Click(object sender, EventArgs e)
48: {
49: listAllProcess();
50: }
51:
52: private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
53: {
54: ls.Clear();
55: listBox2.Items.Clear();
56: pName = listBox1.SelectedItem.ToString();
57: label3.Text = pName;
58:
59: p = Process.GetProcessesByName(pName);
60: try
61: {
62: ProcessModuleCollection pmc = p[0].Modules;
63: foreach (ProcessModule pm in pmc)
64: {
65: ls.Add(pm.FileName);
66: }
67: ls.Sort();
68:
69: foreach (string s in ls)
70: {
71: listBox2.Items.Add(s);
72: }
73: }
74: catch (Exception ex)
75: {
76: MessageBox.Show(ex.Message);
77: }
78: }
79:
80: private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
81: {
82: dllName = listBox2.SelectedItem.ToString();
83: string dllVersion = null;
84: string dllFileSize = null;
85: string dllFileDescription = null;
86: string pPoductName = null;
87: string pVersion = null;
88: string pCopyright = null;
89: string pCompanyName = null;
90:
91: p = Process.GetProcessesByName(pName);
92: ProcessModuleCollection pmc = p[0].Modules;
93: foreach (ProcessModule pm in pmc)
94: {
95: if (String.Compare(pm.FileName, dllName) == 0)
96: {
97: try
98: {
99: dllVersion = pm.FileVersionInfo.FileVersion.Trim();
100: dllFileSize = pm.ModuleMemorySize.ToString().Trim();
101: dllFileDescription = pm.FileVersionInfo.FileDescription.Trim();
102: pPoductName = pm.FileVersionInfo.ProductName.Trim();
103: pVersion = pm.FileVersionInfo.ProductVersion.Trim();
104: pCopyright = pm.FileVersionInfo.LegalCopyright.Trim();
105: pCompanyName = pm.FileVersionInfo.CompanyName.Trim();
106: }
107: catch (Exception)
108: {
109: ;
110: }
111: }
112: }
113:
114: string msg = String.Format(
115: "Dll 名稱: {0} \nDll 版本: {1} \nDll 檔案大小: {2} \nDll 檔案描述: {3} \n \n產品名稱: {4} \n產品版本: {5} \n \n公司名稱: {6} \n版權: {7}",
116: dllName, dllVersion, dllFileSize, dllFileDescription, pPoductName, pVersion, pCompanyName, pCopyright);
117:
118: MessageBox.Show(msg, "詳細資料", MessageBoxButtons.OK, MessageBoxIcon.Information);
119: }
120:
121: private void Form1_FormClosing(object sender, FormClosingEventArgs e)
122: {
123: Properties.Settings ps = Properties.Settings.Default;
124: ps.Size = Size;
125: ps.Location = Location;
126: ps.Save();
127: }
128: }
129: }