2011-10-10 100 views
0

我正在閱讀由Jeffrey Richter編寫的Applied Microsoft .NET Framework Programming。如何檢查在Windows系統上安裝了多少.Net應用程序?

我想知道在Windows系統上安裝了多少.Net應用程序的常用方法。

你能提供意見嗎?

謝謝!

+2

那些使用XCopy部署部署的應用程序的情況如何?這些綠色應用程序不會更改註冊表中的任何內容,也不會在「控制面板」中的「已安裝程序」中列出。您將無法捕捉到這些... –

+0

卸載.net框架並計算彈出多少錯誤消息。 – FreeSnow

回答

1

這幾乎不可能發現。你怎麼知道,有多少正常應用程序安裝?什麼是應用程序,部分是用DotNet編寫的?用DotNet編寫的庫是一個應用程序還是不是?

+0

更別說沒有安裝的應用程序,只是一個可以運行的'exe'。 – RvdK

1

您可以編寫一個程序運行磁盤並掃描.EXE文件,試圖確定它是否是.NET或本機。這裏是一塊的C#代碼,可確定是否一個文件是一個.NET文件(DLL或EXE):

public static bool IsDotNetFile(string filePath) 
{ 
    if (filePath == null) 
     throw new ArgumentNullException("filePath"); 

    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
    { 
     using (BinaryReader br = new BinaryReader(fs)) 
     { 
      if (br.ReadUInt16() != 0x5A4D) // IMAGE_DOS_SIGNATURE 
       return false; 

      byte[] bytes = new byte[112]; // max size we'll need 
      const int dosHeaderSize = (30 - 1) * 2; // see IMAGE_DOS_HEADER 
      if (br.Read(bytes, 0, dosHeaderSize) < dosHeaderSize) 
       return false; 

      fs.Seek(br.ReadUInt32(), SeekOrigin.Begin); 
      if (br.ReadUInt32() != 0x4550) // IMAGE_NT_SIGNATURE 
       return false; 

      // get machine type 
      ushort machine = br.ReadUInt16(); // see IMAGE_FILE_HEADER 

      br.Read(bytes, 0, 20 - 2); // skip the rest of IMAGE_FILE_HEADER 

      // skip IMAGE_OPTIONAL_HEADER 
      if (machine == 0x8664) //IMAGE_FILE_MACHINE_AMD64 
      { 
       br.Read(bytes, 0, 112); // IMAGE_OPTIONAL_HEADER64 
      } 
      else 
      { 
       br.Read(bytes, 0, 96); // IMAGE_OPTIONAL_HEADER32 
      } 

      // skip 14 data directories, and get to the IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, the 15th one 
      br.Read(bytes, 0, 14 * 8); 

      // if the COR descriptor size is 0, it's not .NET 
      uint va = br.ReadUInt32(); 
      uint size = br.ReadUInt32(); 
      return size > 0; 
     } 
    } 
} 

它基於PE標準文件格式分析,但僅集中.NET判定。有關此處的更多信息,請參閱:An In-Depth Look into the Win32 Portable Executable File Format

相關問題