- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在使用 FileVersionInfo.GetVersionInfo() 时遇到了一些严重的问题,希望有人能提供帮助。
问题的基本原理是我正在遍历一个文件夹中的所有文件,每个文件都调用 GetVersionInfo()。大约有300个文件。这适用于除 2 个文件之外的所有文件。对于这些 DLL,我从 GetVersionInfo() 返回的信息完全不正确。
为了消除所有其他变量,我将此调用提取到一个简单的测试应用程序中,但它仍然遇到同样的问题。但是,如果我将测试应用程序构建为 Windows 应用程序(它最初是一个控制台应用程序),那么返回的数据是正确的。
澄清一下,当作为控制台应用程序运行时返回的不正确数据不仅仅是空信息,就像您在文件不包含版本数据时会得到的那样。它包含合理的数据,但只是错误的数据。就好像它是从另一个文件中读取它一样。我已查找包含匹配版本数据的文件,但找不到。
如果构建为控制台应用程序而不是 Windows 应用程序,为什么这个简单的调用功能不同?
如果有人可以提供帮助,我将不胜感激。
Rgds,安迪
-- 添加的代码
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 的资源,确保版本信息只有一个语言/代码页。我希望它可能会为您指明解决方案。
关于c# - 控制台应用程序中的 FileVersionInfo.GetVersionInfo() 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221598/
有了FileVersionInfo类就很容易读取文件的额外信息,但是我有什么办法可以改变它吗? 谢谢。 最佳答案 .NET Framework 中没有可让您轻松完成此操作的类。文件版本数据存储为 na
我有一些经过合理尝试和测试的代码,它们使用 Windows API 调用来读取 FileVersionInfo 字符串,例如“FileVersion”和“CompanyName”。 我发现一个特定的第
我正在尝试使用 C# 和 System.Diagnostics.FileVersionInfo从文件列表中提取版本信息。我这样做的目的是跟踪唯一的文件路径和版本组合。当文件发生变化时,我希望根据具体发
为什么 System.Diagnostics.FileVersionInfo.GetVersionInfo() 返回意外的文件版本信息?我正在查找有关 MPIO 驱动程序的版本信息。目标操作系统是 S
为什么当加载 .NET DLL、从另一个应用程序域替换(DLL 更新为新版本),然后重新加载(使用 Assembly.LoadFrom)版本信息仍然反射(reflect)旧版本? assembly.G
鉴于 Blah.dll 的 AssemblyInfo.cs 中的这段代码: [assembly: AssemblyVersion("3.3.3.3")] [assembly: AssemblyFile
我有一个在 c# 中执行的程序,有时会在运行时通过将 exe 交换为新程序来更新。我希望程序定期检查它是否已更新,如果是,则重新启动。我使用以下函数来执行此操作。 public static
我在使用 FileVersionInfo.GetVersionInfo() 时遇到了一些严重的问题,希望有人能提供帮助。 问题的基本原理是我正在遍历一个文件夹中的所有文件,每个文件都调用 GetVer
在 Silverlight 3.0 应用程序中,我想使用 AssemblyFileVersion显示应用程序的版本信息。这与 AssemblyVersion 不一样并且通常使用以下代码在 .NET 应
我在 Windows 中有一个 SFX(自解压可执行文件)文件(使用 7z、WinRar 等 zip 工具创建,...),其中包含以下详细信息: 我想在C#中获取CopyRight文本,所以我写了下面
我可以通过 C# 中的命名空间 FileVersionInfo 获取文件版本信息。在 FileVersionInfo 中,如果文件版本为空,则我们无法获取其他信息,如产品版本、公司名称等。当我们右键单
在 MSDN Diagnostics.FileVersionInfo它说,例如,ProductMinorPart返回一个整数或 null如果该值未定义。但是它不是 Nullable - 它如何返回空值
我是一名优秀的程序员,十分优秀!