And*_*ndy 4 .net c# system.diagnostics console-application getfileversion
我使用FileVersionInfo.GetVersionInfo()得到一些严重的怪异,并希望有人可以提供帮助.
问题的基础是我正在遍历每个文件夹中调用GetVersionInfo()的所有文件.大约有300个文件.这适用于除2个文件之外的所有文件.对于这些DLL,我从GetVersionInfo()得到了完全不正确的信息.
为了消除所有其他变量,我将此调用提取到一个简单的测试应用程序中,它仍然遇到了同样的问题.但是,如果我将测试应用程序构建为Windows应用程序(最初是控制台应用程序),那么数据就会恢复正常.
只是为了澄清一下,当作为控制台应用程序运行时返回的不正确数据不仅仅是空信息,就像文件不包含版本数据一样.它包含合理的数据,但只包含错误的数据.就好像是从不同的文件中读取它一样.我找了一个包含匹配版本数据的文件,但找不到.
如果构建为控制台应用程序而不是Windows应用程序,为什么这个简单的调用功能会不同?
如果有人可以帮助我,我将非常感激.
安德,安迪
- 已添加代码
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种行为确实很奇怪.可能是控制台应用程序没有像WinForms应用程序那样从同一个地方加载DLL吗?这意味着GetVersionInfo使用一些其他API而不仅仅是Win32 CreateFile(可能会通过一些DLL解析器机制,并排或其他); 请记住,在幕后,version.dll将执行您的请求,而不是CLR本身.
纵观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;
}
Run Code Online (Sandbox Code Playgroud)
正如你在那里看到的那样,一些有趣的舞蹈正在进行着代码页.如果您检查的DLL附加了多个版本信息资源怎么办?根据程序调用的文化GetVersionInfo,我想代码页相关的调用可以返回其他结果?
花点时间检查DLL的资源,并确保版本信息只有一个语言/代码页.我希望,它可能会指出你的解决方案.