You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7 C#与PowerShell获取系统文件版本不一致问题咨询

问题原因分析

这个差异的核心原因是Windows WOW64文件系统重定向机制:

  • 当你使用32位的.NET 7程序运行时,操作系统会自动将对C:\Windows\System32目录的访问重定向到C:\Windows\SysWOW64,该目录存放的是32位版本的系统文件,其中notepad.exe、calc.exe这类系统程序的版本确实为6.2(对应旧版32位程序)。
  • PowerShell在64位系统上默认运行64位版本,它直接访问C:\Windows\System32目录下的64位系统文件,这些文件的版本为10.0,因此能返回正确的版本号。
  • 其他普通.exe文件不存在32/64位版本的重定向差异,所以不会出现这类问题。

验证与解决方法

  1. 将.NET程序编译为64位(在项目属性中设置平台目标为x64),重新运行即可获取10.0的版本号。
  2. 在32位程序中手动关闭WOW64重定向后访问System32目录,也能获取正确版本,示例代码:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

    static void Main()
    {
        IntPtr ptr = IntPtr.Zero;
        try
        {
            Wow64DisableWow64FsRedirection(ref ptr);
            var versionInfo = FileVersionInfo.GetVersionInfo(@"C:\Windows\System32\notepad.exe");
            Console.WriteLine($"FileMajorPart: {versionInfo.FileMajorPart}, FileMinorPart: {versionInfo.FileMinorPart}");
        }
        finally
        {
            if (ptr != IntPtr.Zero)
            {
                Wow64RevertWow64FsRedirection(ptr);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者0xdeadbeef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 00:42:05