无论.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).
Is there a way to programmatically get the FileInfo/Path of the ildasm.exe/ilasm.exe executables? I'm attempting to decompile and recompile a dll/exe file appropriately after making some alterations to it (I'm guessing PostSharp does something similar to alter the IL after the 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");
...
但是,当我运行此代码时,该目录不存在(主要是因为我的SDK版本是7.1),因此在我的本地计算机上,正确的路径是@"Microsoft SDKs\Windows\v7.1\bin"
.如何确保可以真正找到ildasm.exe?
However, when I ran this code the directory did not exist (mainly because my SDK version is 7.1), so on my local machine the correct path is @"Microsoft SDKs\Windows\v7.1\bin"
. How do I ensure I can actually find the ildasm.exe?
类似地,我找到了另一篇有关如何以以下方式访问ilasm.exe的博客文章:
Similarly, I found another blog post on how to get access to ilasm.exe as:
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版本吗?
While this works, I noticed that I have Framework and Framework64, and within Framework itself I have all of the versions up to v4.0.30319 (same with Framework64). So, how do I know which one to use? Should it be based on the .NET Framework version I'm targetting?
摘要:
- 如何适当保证找到ildasm.exe的正确路径?
- 如何正确选择要编译的正确ilasm.exe?
推荐答案
一种选择是引用Microsoft.Build.Utilities.Core
并使用:
One option would be to reference 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
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
这篇关于无论.NET版本/环境如何,都能找到ildasm.exe和ilasm.exe文件的文件路径的保证方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!