2010-01-30 67 views
6

如何確定一個特定的.Net程序集是否已被創建?我需要從代碼檢查。即使調用命令行也沒問題。目前我看不出有什麼辦法來確定這一點。如何確定裝配是否已經完成?

+0

在代碼中?從命令行?交互式?什麼?它必須在文件屬性的某處... – 2010-01-30 09:31:11

回答

3

您可以嘗試在「ngen cache」(C:\ Windows \ assembly \ NativeImages_v2XXXXXXX)中找到您的程序集。

已存儲的程序集將具有以下格式名稱: [basename]。 ni。[baseextension]

+0

Windows 7似乎存儲它們是一個稍微不同的位置,並沒有'.ni。'在名字中:C:\ Windows \ assembly – 2010-02-16 19:16:41

+0

很奇怪。我安裝了Windows 7,「ngen cash」路徑是「C:\ Windows \ assembly \ NativeImages_v2.0.50727_32 \」。 – Sasha 2010-02-17 08:02:50

4

檢查從代碼

檢查,如果我們加載本地圖像執行的程序集。我在加載的模塊文件名屬性中尋找模式「\ assemblyname.ni」。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
using System.Diagnostics; 

namespace MyTestsApp 
{ 
    class Program 
    { 
     static bool Main(string[] args) 
     { 

      Process process = Process.GetCurrentProcess(); 

      ProcessModule[] modules = new ProcessModule[process.Modules.Count]; 
      process.Modules.CopyTo(modules,0); 

      var niQuery = from m in modules where m.FileName.Contains("\\"+process.ProcessName+".ni") select m.FileName; 
      bool ni = niQuery.Count()>0 ?true:false; 

      if (ni) 
      { 
       Console.WriteLine("Native Image: "+niQuery.ElementAt(0)); 
      } 
      else 
      { 
       Console.WriteLine("IL Image: " + process.MainModule.FileName); 
      } 

      return ni; 
     } 
    } 
} 

命令行解決方案:在命令提示

運行 「NGEN顯示」。

例子:

NGEN顯示MyTestsApp.exe

如果已安裝,它打印出像 機映像: MyTestsApp,版本= 1.0.0.0,文化=中立,公鑰=空

並返回0(%ERRORLEVEL%)

否則,它打印出:

錯誤:指定的程序集未安裝。

並返回-1

相關問題