2007年05月8日 10:10

C# - Dll Viewer 檢視程式執行所需組件

Dll Viewer

在 PTT 的 C# 板上,看到有人發問如何偵測某個程式,是否正在執行?

答案就是,使用 System.Diagnostics 命名空間的 Process 類別,就可以使用 GetProcessesByName 方法取得程式。

這讓我想到以前我在想的問題:是不是有程式幫助我們列出,某個程式所使用到的組件詳細資料,也就是 exe 與 dll 資料?

沒有想到,現在利用 .NET 2.0 就可以輕鬆寫出一個這樣的程式。除了上述的 Process 類別,再加上 ProcessModuleProcessModuleCollection 即可。

程式執行畫面:


(1) 程式開啟後,就會載入目前正在執行的程式,按左上角的【更新】按鈕,即可重新整理。

Dll Viewer 畫面 01

 
 (2) 點擊左方畫面中的程式,就會列出該程式執行所需要的組件。

Dll Viewer 畫面 02


(3) 點擊組件名稱,就會跳出組件的詳細資料。

Dll Viewer 畫面 03

 
程式碼:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Windows.Forms;
  5. namespace DllViewer
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         private string pName = null;
  10.         private string dllName = null;
  11.         private Process[] p = null;
  12.         private List<string> ls = new List<string>();
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void listAllProcess()
  18.         {
  19.             ls.Clear();
  20.             listBox1.Items.Clear();
  21.             p = Process.GetProcesses();
  22.             foreach (Process pValue in p)
  23.             {
  24.                 ls.Add(pValue.ProcessName.ToString());
  25.             }
  26.             ls.Sort();
  27.             foreach (string s in ls)
  28.             {
  29.                 listBox1.Items.Add(s);
  30.             }
  31.         }
  32.         private void Form1_Load(object sender, EventArgs e)
  33.         {
  34.             toolTip1.SetToolTip(button1, "更新");
  35.             Properties.Settings ps = Properties.Settings.Default;
  36.             this.Size = ps.Size;
  37.             this.Location = ps.Location;
  38.             listAllProcess();
  39.         }
  40.         private void button1_Click(object sender, EventArgs e)
  41.         {
  42.             listAllProcess();
  43.         }
  44.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  45.         {
  46.             ls.Clear();
  47.             listBox2.Items.Clear();
  48.             pName = listBox1.SelectedItem.ToString();
  49.             label3.Text = pName;
  50.             p = Process.GetProcessesByName(pName);
  51.             try
  52.             {
  53.                 ProcessModuleCollection pmc = p[0].Modules;
  54.                 foreach (ProcessModule pm in pmc)
  55.                 {
  56.                     ls.Add(pm.FileName.ToString());
  57.                 }
  58.                 ls.Sort();
  59.                 foreach (string s in ls)
  60.                 {
  61.                     listBox2.Items.Add(s);
  62.                 }
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 MessageBox.Show(ex.Message);
  67.             }
  68.         }
  69.         private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
  70.         {
  71.             dllName = listBox2.SelectedItem.ToString();
  72.             string dllVersion = null;
  73.             string dllFileSize = null;
  74.             string dllFileDescription = null;
  75.             string pPoductName = null;
  76.             string pVersion = null;
  77.             string pCopyright = null;
  78.             string pCompanyName = null;
  79.             p = Process.GetProcessesByName(pName);
  80.             ProcessModuleCollection pmc = p[0].Modules;
  81.             foreach (ProcessModule pm in pmc)
  82.             {
  83.                 if (String.Compare(pm.FileName.ToString(), dllName) == 0)
  84.                 {
  85.                     try
  86.                     {
  87.                         dllVersion = pm.FileVersionInfo.FileVersion.ToString().Trim();
  88.                         dllFileSize = pm.ModuleMemorySize.ToString().Trim();
  89.                         dllFileDescription = pm.FileVersionInfo.FileDescription.ToString().Trim();
  90.                         pPoductName = pm.FileVersionInfo.ProductName.ToString().Trim();
  91.                         pVersion = pm.FileVersionInfo.ProductVersion.ToString().Trim();
  92.                         pCopyright = pm.FileVersionInfo.LegalCopyright.ToString().Trim();
  93.                         pCompanyName = pm.FileVersionInfo.CompanyName.ToString().Trim();
  94.                     }
  95.                     catch (Exception)
  96.                     {
  97.                         ;
  98.                     }
  99.                 }
  100.             }
  101.             string msg = String.Format(
  102.                     "Dll 名稱: {0} \nDll 版本: {1} \nDll 檔案大小: {2} \nDll 檔案描述: {3} \n\n產品名稱: {4} \n產品版本: {5} \n\n公司名稱: {6} \n版權: {7}",
  103.                     dllName, dllVersion, dllFileSize, dllFileDescription, pPoductName, pVersion, pCompanyName, pCopyright);
  104.             MessageBox.Show(msg, "詳細資料", MessageBoxButtons.OK, MessageBoxIcon.Information);
  105.         }
  106.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  107.         {
  108.             Properties.Settings ps = Properties.Settings.Default;
  109.             ps.Size = this.Size;
  110.             ps.Location = this.Location;
  111.             ps.Save();
  112.         }
  113.     }
  114. }

下載完整程式碼與執行程式


  • chhuang0123 發表於樂多回應(1)引用(0)C# 編輯本文
    樂多分類:網路/3C │昨日人次:0 │累計人次:161
    Ads by Roodo! 

    引用URL

    http://cgi.blog.roodo.com/trackback/3158529
    回應文章

    感謝分享!!!!
    | 檢舉 | Posted by WL at 2008年12月12日 09:22