4

拿别人的 Program Files 文件夹?别忘了考虑 x86/x64 路径

 3 years ago
source link: https://blog.walterlv.com/post/get-program-files-cross-x64-x86.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

拿别人的 Program Files 文件夹?别忘了考虑 x86/x64 路径

2020-06-03 00:13

要拿适用于自己进程的 Program Files 文件夹很简单,无脑拿就好了。不过,如果涉及到拿其他程序的,那么就会涉及到与其他程序不同架构时路径不同的问题。


Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) 即可用来获取 Program Files 文件夹的路径。从 .NET Framework 4.0 开始,还增加了一个 ProgramFilesX86 枚举可用。

在官方文档中,ProgramFiles 枚举拿的是当前进程架构下的 Program Files 文件夹,ProgramFilesX86 拿的是 x86 进程架构下的 Program Files 文件夹。

为了具体说明,可以用下面的示例程序:

using System;

namespace Walterlv.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var is64Bit = Environment.Is64BitProcess;
            var pfx86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
            var pf = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            Console.WriteLine($"process = {(is64Bit ? "x64" : "x86")}");
            Console.WriteLine($"x86     = {pfx86}");
            Console.WriteLine($"current = {pf}");
        }
    }
}

在 x64 系统下,输出是:

process = x64
x86     = C:\Program Files (x86)
current = C:\Program Files

在 x86 系统下,输出是:

process = x86
x86     = C:\Program Files (x86)
current = C:\Program Files (x86)

所以,只是通过此属性的话,x86 进程不能获取到 x64 进程的目录。


参考资料

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/get-program-files-cross-x64-x86.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK