控制台应用程序中的FileVersionInfo.GetVersionInfo()不正确

我使用FileVersionInfo.GetVersionInfo()得到了一些严重的怪异,并希望有人可以提供帮助。

问题的基础是我正在遍历每个文件夹中调用GetVersionInfo()的所有文件。 大约有300个文件。 这适用于除2个文件之外的所有文件。 对于这些DLL,我从GetVersionInfo()得到了完全不正确的信息。

为了消除所有其他变量,我将此调用提取到一个简单的测试应用程序中,它仍然遇到了同样的问题。 但是,如果我将测试应用程序构建为Windows应用程序(最初是控制台应用程序),那么数据会恢复正常。

只是为了澄清一下,当作为控制台应用程序运行时返回的不正确数据不仅仅是如果文件不包含版本数据就会得到的空信息。 它包含合理的数据,但只包含错误的数据。 就好像是从不同的文件中读取它一样。 我找了一个包含匹配版本数据的文件,但找不到。

如果构建为控制台应用程序而不是Windows应用程序,为什么这个简单的调用function会不同?

如果有人可以帮助我,我将非常感激。

安德,安迪

– 已添加代码

using System; using System.Diagnostics; namespace test { class Program { static void Main(string[] args) { string file = "C:\\ProblemFile.dll"; FileVersionInfo version = FileVersionInfo.GetVersionInfo(file); string fileName = version.FileName; string fileVersion = version.FileVersion; Console.WriteLine(string.Format("{0} : {1}", fileName, fileVersion)); } } } 

这种行为确实很奇怪。 可能是控制台应用程序没有像WinForms应用程序那样从同一个地方加载DLL吗? 这意味着GetVersionInfo使用一些其他API而不仅仅是Win32 CreateFile (可能会通过一些DLL解析器机制,并排或其他); 记住,在封面下, version.dll将执行您的请求,而不是CLR本身。

通过Reflector指向另一个方向的FileVersionInfo

 public static unsafe FileVersionInfo GetVersionInfo(string fileName) { // ... int fileVersionInfoSize = UnsafeNativeMethods.GetFileVersionInfoSize(fileName, out num); FileVersionInfo info = new FileVersionInfo(fileName); if (fileVersionInfoSize != 0) { byte[] buffer = new byte[fileVersionInfoSize]; fixed (byte* numRef = buffer) { IntPtr handle = new IntPtr((void*) numRef); if (!UnsafeNativeMethods.GetFileVersionInfo(fileName, 0, fileVersionInfoSize, new HandleRef(null, handle))) { return info; } int varEntry = GetVarEntry(handle); if (!info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(varEntry))) { int[] numArray = new int[] { 0x40904b0, 0x40904e4, 0x4090000 }; foreach (int num4 in numArray) { if ((num4 != varEntry) && info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(num4))) { return info; } } } } } return info; } 

正如你在那里看到的那样,一些有趣的舞蹈正在进行着代码页。 如果您检查的DLL有多个附加的版本信息资源怎么办? 根据调用GetVersionInfo的程序的文化,我想与代码页相关的调用可以返回其他结果?

花点时间检查DLL的资源,并确保版本信息只有一个语言/代码页。 我希望,它可能会指出你的解决方案。

当然你看到的“文件”不是。 和..? 如果您遍历所有文件,您将始终看到条目。 (当前目录)和..(向上dir)。 GetVersion Info可能会为这些返回任何内容。 您必须按名称手动过滤这些条目。

文件和汇编版本是两个不同的东西。

你确定你不期待对方吗?

更新:试过这个。 没工作。

 using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace test { class Program { [DllImport("COMCTL32")] private static extern int InitCommonControls(int nExitCode); static void Main(string[] args) { InitCommonControls(0); string file = "C:\\ProblemFile.dll"; FileVersionInfo version = FileVersionInfo.GetVersionInfo(file); string fileName = version.FileName; string fileVersion = version.FileVersion; Console.WriteLine(string.Format("{0} : {1}", fileName, fileVersion)); } } }