簡體   English   中英

無論.NET版本/環境如何,保證找到ildasm.exe和ilasm.exe文件的文件路徑的方法?

[英]Guaranteed way to find the filepath of the ildasm.exe and ilasm.exe files regardless of .NET version/environment?

有沒有辦法以編程方式獲取ildasm.exe / ilasm.exe可執行文件的FileInfo / Path? 我正在嘗試反編譯並重新編譯一個dll / exe文件后對其進行一些修改(我猜測PostSharp會做類似的事情,在編譯后改變IL)。

我發現了一篇博文,指出:

var pfDir = Environment.GetFolderPath(Environment.SpecialFolders.ProgramFiles));
var sdkDir = Path.Combine(pfDir, @"Microsoft SDKs\Windows\v6.0A\bin");

...

但是,當我運行此代碼時,目錄不存在(主要是因為我的SDK版本是7.1),因此在我的本地計算機上正確的路徑是@"Microsoft SDKs\\Windows\\v7.1\\bin" 我怎樣才能確保我能找到ildasm.exe?

同樣,我發現了另一篇關於如何訪問ilasm.exe的博客文章:

string windows = Environment.GetFolderPath(Environment.SpecialFolder.System);
string fwork = Path.Combine(windows, @"..\Microsoft.NET\Framework\v2.0.50727");

...

雖然這有效,但我注意到我有Framework和Framework64,而在Framework本身中我擁有v4.0.30319的所有版本(與Framework64相同)。 那么,我怎么知道使用哪一個? 它應該基於我正在瞄准的.NET Framework版本嗎?

摘要:

  • 我如何保證找到到ildasm.exe的正確路徑?
  • 如何正確選擇正確的ilasm.exe進行編譯?

一種選擇是引用Microsoft.Build.Utilities.Core並使用:

var ildasm = Microsoft.Build.Utilities.ToolLocationHelper.GetPathToDotNetFrameworkSdkFile("ildasm.exe", TargetDotNetFrameworkVersion.VersionLatest);
var ilasm = Microsoft.Build.Utilities.ToolLocationHelper.GetPathToDotNetFrameworkFile("ilasm.exe", TargetDotNetFrameworkVersion.VersionLatest);

現在我的機器上返回:

ildasm = C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools\\ildasm.exe

ilasm = C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\ilasm.exe

我最近需要這樣做,所以我搜索了Windows SDK的所有可能路徑的互聯網,並搜索最近已知順序的那些。 我還檢查操作系統和進程是否為64位,然后通過查看相應的Program Files文件夾來使用該版本。 我不認為在32位版本的工具上選擇64位有任何重要意義。 IL86m的x86版本可以很快地組裝64位首選程序集,它們都是IL而不是實際執行任何代碼。

ILDasm是Windows SDK的一部分,其中ILAsm只是.NET Framework SDK,所以這里有一些靜態方法來追捕它們。 代碼是針對.NET 4.0的,但如果你願意的話,你可以做一些小的調整來使它在.NET 2.0上構建。

// ILDasm.exe will be somewhere in here
private static string FindPathForWindowsSdk()
{
  string[] windowsSdkPaths = new[]
  {
    @"Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\",
    @"Microsoft SDKs\Windows\v8.0A\bin\",
    @"Microsoft SDKs\Windows\v8.0\bin\NETFX 4.0 Tools\",
    @"Microsoft SDKs\Windows\v8.0\bin\",
    @"Microsoft SDKs\Windows\v7.1A\bin\NETFX 4.0 Tools\",
    @"Microsoft SDKs\Windows\v7.1A\bin\",
    @"Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\",
    @"Microsoft SDKs\Windows\v7.0A\bin\",
    @"Microsoft SDKs\Windows\v6.1A\bin\",        
    @"Microsoft SDKs\Windows\v6.0A\bin\",
    @"Microsoft SDKs\Windows\v6.0\bin\",
    @"Microsoft.NET\FrameworkSDK\bin"
  };

  foreach (var possiblePath in windowsSdkPaths)
  {
    string fullPath = string.Empty;

    // Check alternate program file paths as well as 64-bit versions.
    if (Environment.Is64BitProcess)
    {
      fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), possiblePath, "x64");
      if (Directory.Exists(fullPath))
      {
        return fullPath;
      }

      fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), possiblePath, "x64");
      if (Directory.Exists(fullPath))
      {
        return fullPath;
      }
    }

    fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), possiblePath);
    if (Directory.Exists(fullPath))
    {
      return fullPath;
    }

    fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), possiblePath);
    if (Directory.Exists(fullPath))
    {
      return fullPath;
    }
  }

  return null;
}

// ILAsm.exe will be somewhere in here
private static string FindPathForDotNetFramework()
{
  string[] frameworkPaths = new[]
  {
    @"Microsoft.NET\Framework\v4.0.30319",
    @"Microsoft.NET\Framework\v2.0.50727"
  };

  foreach (var possiblePath in frameworkPaths)
  {
    string fullPath = string.Empty;

    if (Environment.Is64BitProcess)
    {
      fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), possiblePath.Replace(@"\Framework\", @"\Framework64\"));
      if (Directory.Exists(fullPath))
      {
        return fullPath;
      }
    }

    fullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), possiblePath);
    if (Directory.Exists(fullPath))
    {
      return fullPath;
    }
  }

  return null;
}

您可以通過傳入您正在尋找的可執行文件來擴充它,並使用File.Exists更改Directory.Exists ,由您決定。 您還可以獲取可能的列表並將它們放在配置文件中,這樣您就可以添加更多而無需重新編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM