uwp app安装在目标机System.Diagnostics.FileVersionInfo.GetVersionInfo 无法读取一些exe信息

Yi Wang (Shanghai Wicresoft Co Ltd) 0 信誉分 Microsoft 供应商
2024-05-10T06:55:01.6166667+00:00

uwp app安装在目标机System.Diagnostics.FileVersionInfo.GetVersionInfo 无法读取一些exe信息。

开发环境或是vs下可以读取。安装客户机上就是无法读取,报错 返回 exe的路径。

我有试用powershell 读取,一样。开发vs可以客户机不可以。我有查找网络,

有的讲是沙盒用的权限问题。 提高权限,(我已在客户机使用管理员权限启动,无果)

代码如下

try

{

var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);

var fileInfo = new FileInfo(filePath);

newApp.AppVersion = $"{versionInfo.ProductMajorPart}.{versionInfo.ProductMinorPart}.{versionInfo.ProductBuildPart}.{versionInfo.ProductPrivatePart}";

newApp.InstallDate = fileInfo.CreationTime;

}

catch (Exception ex)

{

LogService.WriteLog($"{newApp.AppName};{ex.message};[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取");

}

实时字幕;C:\Windows\system32\LiveCaptions.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

Windows 内存诊断;C:\Windows\system32\MdSched.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

系统配置;C:\Windows\system32\msconfig.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

讲述人;C:\Windows\system32\narrator.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

屏幕键盘;C:\Windows\system32\osk.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

打印管理;C:\Windows\system32\pmcsnap.dll;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

恢复驱动器;C:\Windows\system32\RecoveryDrive.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

服务;C:\Windows\system32\services.exe;[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

语音访问;C:\Windows\system32\voiceaccess.exe[System.Diagnostics.FileVersionInfo.GetVersionInfo]无法读取

通用 Windows 平台 (UWP)
通用 Windows 平台 (UWP)
一个 Microsoft 平台,用于生成和发布适用于 Windows 桌面设备的应用。
29 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
135 个问题
0 个注释 无注释
{count} 票

2 个答案

排序依据: 非常有帮助
  1. Yi Wang (Shanghai Wicresoft Co Ltd) 0 信誉分 Microsoft 供应商
    2024-05-15T03:29:04.2533333+00:00

    在C#中,当您尝试访问 C:\Windows\System32 目录下的文件时,可能会遇到一些特定的问题,尤其是在使用32位应用程序运行在64位操作系统上时。因为在这种情况下,Windows会将对 System32 的访问重定向到 C:\Windows\SysWOW64

    要解决这个问题,您可以尝试以下几种方法:

    方法1:确保以64位模式运行应用程序

    如果您的应用程序是以32位模式编译的,尝试将其编译为64位模式,以避免路径重定向问题。

    方法2:使用环境变量获取系统目录

    使用 Environment.SystemDirectory 可以确保您获取的是正确的系统目录路径。

    方法3:禁用文件系统重定向(仅在需要时使用)

    在极少数情况下,您可能需要禁用文件系统重定向。以下是一个示例,但请谨慎使用这种方法:

    
    using System;
    
    using System.IO;
    
    using System.Runtime.InteropServices;
    
    class Program
    
    {
    
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    
        static extern IntPtr LoadLibrary(string lpFileName);
    
        [DllImport("kernel32.dll", SetLastError = true)]
    
        [return: MarshalAs(UnmanagedType.Bool)]
    
        static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    
        [DllImport("kernel32.dll", SetLastError = true)]
    
        [return: MarshalAs(UnmanagedType.Bool)]
    
        static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    
        static void Main()
    
        {
    
            string system32Path = Environment.SystemDirectory;
    
            string filePath = Path.Combine(system32Path, "XX.exe");
    
            IntPtr wow64Value = IntPtr.Zero;
    
            bool isWow64FsRedirectionDisabled = Wow64DisableWow64FsRedirection(ref wow64Value);
    
            try
    
            {
    
                if (File.Exists(filePath))
    
                {
    
                    Console.WriteLine("文件存在!");
    
                    // 读取文件或执行其他操作
    
                }
    
                else
    
                {
    
                    Console.WriteLine("文件不存在。");
    
                }
    
            }
    
            catch (Exception ex)
    
            {
    
                Console.WriteLine($"操作失败: {ex.Message}");
    
            }
    
            finally
    
            {
    
                if (isWow64FsRedirectionDisabled)
    
                {
    
                    Wow64RevertWow64FsRedirection(wow64Value);
    
                }
    
            }
    
        }
    
    }
    
    

    方法4:检查文件路径和权限

    确保您拥有访问该文件的权限,并且文件路径是正确的。如果文件路径或权限有问题,也会导致无法找到文件的错误。

    示例代码

    综合以上建议,以下是一个示例代码,展示了如何正确读取 C:\Windows\System32\XX.exe 文件:

    
    using System;
    
    using System.IO;
    
    class Program
    
    {
    
        static void Main()
    
        {
    
            try
    
            {
    
                string system32Path = Environment.SystemDirectory;
    
                string filePath = Path.Combine(system32Path, "XX.exe");
    
                if (File.Exists(filePath))
    
                {
    
                    Console.WriteLine("文件存在!");
    
                    // 读取文件或执行其他操作
    
                    string fileContent = File.ReadAllText(filePath);
    
                    Console.WriteLine(fileContent);
    
                }
    
                else
    
                {
    
                    Console.WriteLine("文件不存在。");
    
                }
    
            }
    
            catch (Exception ex)
    
            {
    
                Console.WriteLine($"操作失败: {ex.Message}");
    
            }
    
        }
    
    }
    
    

    以上代码将确保正确获取系统目录,并检查文件是否存在。根据需要,您可以在代码中添加更多逻辑来处理文件读取或其他操作。

    0 个注释 无注释

  2. Deleted

    由于违反了我们的《行为准则》,此答案已被删除。 在采取措施之前,已通过自动检测手动报告或识别该答案。 有关详细信息,请参阅我们的行为准则


    已关闭批注。 了解详细信息