C# 无论目标机器的架构如何,如何获取程序文件的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2284725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:04:04  来源:igfitidea点击:

How do get the path of Program Files regardless of the architecture of the target machine

c#.netspecial-foldersprogram-files

提问by PICyourBrain

I'm programming in C#/.NET. I want to be able to return the Program Files directory from the target machine no matter what the architecture of the target machine is. To clarify, I want it to return C (or whatever drive the OS is on):/Program Filesno matter what bitness their version of Windows is.

我在 C#/.NET 中编程。无论目标机器的架构是什么,我都希望能够从目标机器返回 Program Files 目录。澄清一下,C (or whatever drive the OS is on):/Program Files无论他们的 Windows 版本是多少,我都希望它返回。

I could just hardcode in the directory except if the user was running Windows that's not installed on the C: drive it wouldn't work.

我只能在目录中进行硬编码,除非用户运行的 Windows 未安装在 C: 驱动器上,否则它将无法工作。

I found

我发现

FileInfo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) 

but I think it returns the Program Files (x86)folder on a 64 bit windows machine.

但我认为它返回Program Files (x86)64 位 Windows 机器上的文件夹。

采纳答案by Fredrik M?rk

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)returns "c:\Program Files"on a 64-bit machine, unless the code is build to target x86, in which case it returns "C:\Program Files (x86)", so I guess that would work for you.

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)"c:\Program Files"在 64 位机器上返回,除非代码是针对 x86 构建的,在这种情况下它返回"C:\Program Files (x86)",所以我想这对你有用。