A guaranteed way to find the path to the ildasm.exe and ilasm.exe file regardless of the .NET version / environment?

Is there a way to programmatically get FileInfo / Path from ildasm.exe / ilasm.exe executables? I am trying to decompile and recompile the dll / exe file appropriately after making some changes to it (I assume that PostSharp does something similar to changing the IL after compilation).

I found a blog post that pointed to:

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

However, when I ran this code, the directory did not exist (mainly because my version of the SDK is 7.1), so the correct path is @"Microsoft SDKs\Windows\v7.1\bin" on my local machine. How can I find the ildasm.exe file?

Similarly, I found another blog post on how to access ilasm.exe as:

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

While this works, I noticed that I have Framework and Framework64, and inside the Framework itself I have all versions up to version 4.0.30319 (the same with Framework64). So how do I know which one to use? Should it be based on the version of the .NET Framework that I am targeting?

Summary:

  • How can I guarantee the correct path to the ildasm.exe file?
  • How to choose the right ilasm.exe file to compile?
+8
c # filepath ildasm ilasm
source share
2 answers

One option is to link Microsoft.Build.Utilities.Core and use:

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

Right now on my machine, this returns:

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

+12
source share

I recently needed to do this, so I spent firewalls for all the possible paths of the Windows SDK and looked at those that were found in the most famous order recently. I also check if the OS and the process are 64-bit, and then just use this version by looking at the corresponding Program Files folders. I don’t think that choosing a 64-bit version of 32-bit versions of tools is of great importance. The ILAsm x86 version can happily build 64-bit preferred assemblies without a hitch, this is all IL and does not actually execute any code.

ILDasm is part of the Windows SDK, where ILAsm is just the .NET Framework SDK, so here are some static methods for finding them. The code is baked for .NET 4.0, but you can do some minor tricks to get it on .NET 2.0 if you want.

 // 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; } 

You can increase this by passing to the executable you are looking for, and change Directory.Exists from File.Exists as well, up to you. You can also take possible lists and put them in a configuration file so that you can add more without recompiling.

+6
source share

All Articles