c# - 从大文件中检索版本信息

标签 c# .net

我们有包含自定义版本信息的大型可执行文件(>1.2 GB)。 我尝试使用“FileVersionInfo”类的“GetVersionInfo”从这些文件中检索“版本信息”。 出于某种原因,此方法不会返回 Windows XP 中较大文件(已测试 > 1 GB)的版本信息。它确实适用于大小为 18 MB 的文件。 当我在 Windows 7(x86 和 X64)中尝试相同的操作时,它适用于所有文件,甚至是更大的文件!

我使用 Reflector 工具查看 FileVersionInfo 类,并创建了一个小型控制台应用程序来检索“文件版本信息”- 大小, 就像“GetVersionInfo”方法一样。对于相同的文件,在 Windows XP 中返回大小 0(零),在 Windows 7 中返回大小 1428。 XP 中的最后一个错误是 1812(“指定的图像文件不包含资源部分”)。

这在 Windows XP 中不起作用而在 Windows 7 中起作用的原因是什么? 是否有解决方法来检索版本信息?

在我测试过的代码下面:

class Program
{
    [DllImport("version.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetFileVersionInfoSize(string lptstrFilename, out int handle);

    static void Main(string[] args)
    {
        Console.Write("Path which contains the executables: ");
        string path = Console.ReadLine();

        foreach (string fileName in Directory.EnumerateFiles(path))
        {
            int num;
            int fileVersionInfoSize = GetFileVersionInfoSize(fileName, out num);
            int error = Marshal.GetLastWin32Error();

            Console.WriteLine("File Version Info Size: " + fileVersionInfoSize);

            if (error != 0)
            {
                Console.WriteLine("Last Error: " + error);
            }
        }

        Console.ReadKey();
    }
}

最佳答案

加载器可能无法将整个文件映射到它的地址空间。请随意阅读 PE 文件格式以查找版本资源。

但是,你用这么大的 PE Image 做什么?如果是自定义资源,最好将它们附加到 .exe,这样加载程序只需映射一点,然后直接访问它们。这需要您知道您的大小(在偏移量 124 处有 4 个字节可以安全地覆盖,因为它们在“此程序无法在 MS-DOS 模式下运行”错误消息 stub 之后被填充)。

关于c# - 从大文件中检索版本信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822147/

相关文章:

c# - 我如何按创建日期列出文件

c# - 为什么枚举数组可以转换为两种不同的 IEnumerable<T> 类型?

c# - 使用 LimitedConcurrencyLevelTask​​Scheduler 时继续任务挂起

.net - 将 AutoFac 设置为默认使用 PropertiesAutowired(true)?

c# - 什么时候使用 PreRender 而不是 PageLoad?

MethodInfo 调用中的 C# Lambda

c# - .NET : Create custom control like UItableView in iPhone SDK

c# - 分配引用类型时 c# 中的奇怪 stackoverflow

.net - 加载 SOS 扩展进行调试

.net - 在 VS2010 中编译旧版本的 .net