在 C# 中获取主目录的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1143706/
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
Getting the path of the home directory in C#?
提问by MiffTheFox
Okay, I've checked Environment.SpecialFolder, but there's nothing in there for this.
好的,我已经检查了 Environment.SpecialFolder,但是里面没有任何内容。
I want to get the home directory of the current user in C#. (e.g. c:\documents and settings\user
under XP, c:\users\user
under Vista, and /home/user
under Unix.)
我想在 C# 中获取当前用户的主目录。(例如c:\documents and settings\user
在 XP、c:\users\user
Vista 和/home/user
Unix 下。)
I know I can read enviroment variables to find this out, but I want to do this in a cross-platform way.
我知道我可以通过读取环境变量来找出这一点,但我想以跨平台的方式来做到这一点。
Is there any way I can do this with .NET (preferably using mscorlib)?
有什么办法可以用 .NET 做到这一点(最好使用 mscorlib)?
UPDATE: Okay, this is the code I ended up using:
更新:好的,这是我最终使用的代码:
string homePath = (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX)
? Environment.GetEnvironmentVariable("HOME")
: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
采纳答案by sigint
Environment.SpecialFolder.Personal
doesn't actually return the home folder, it returns the My Documentsfolder. The safest way to get the home folder on Win32 is to read %HOMEDRIVE%%HOMEPATH%
. Reading environment variables is actually very portable to do (across Unix and Windows), so I'm not sure why the poster wanted to notdo it.
Environment.SpecialFolder.Personal
实际上并不返回主文件夹,它返回我的文档文件夹。在 Win32 上获取主文件夹的最安全方法是读取%HOMEDRIVE%%HOMEPATH%
. 阅读环境变量其实是非常移植到(在UNIX和Windows)做的,所以我不知道为什么海报想不去做。
Edited to add:For crossplatform (Windows/Unix) C#, I'd read $HOME
on Unix and OSX and %HOMEDRIVE%%HOMEPATH%
on Windows.
编辑添加:对于跨平台(Windows/Unix)C#,我会$HOME
在 Unix 和 OSX 以及%HOMEDRIVE%%HOMEPATH%
Windows 上阅读。
回答by Matthew Scharley
I believe what you are looking for is:
我相信你正在寻找的是:
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
For reference, it is infact contained in mscorlib
.
作为参考,它实际上包含在mscorlib
.
回答by John D.
When you say cross-platform way, what other OSs do you need to support? You may need to do some simple OS detection to select the method for finding the home directory if you're running on a non-Windows OS.
当您说跨平台方式时,您还需要支持哪些其他操作系统?如果您在非 Windows 操作系统上运行,您可能需要进行一些简单的操作系统检测来选择查找主目录的方法。
This websiteseems to give a way to do what you need in Windows.
该网站似乎提供了一种在 Windows 中执行所需操作的方法。
回答by JP Alioto
I don't have a machine to test it on, but %HOMESHARE%
might work for you. Otherwise, here's a pretty good list of environment variables.
我没有机器来测试它,但%HOMESHARE%
可能对你有用。否则,这里有一个很好的环境变量列表。
回答by AnthonyWJones
The bottom line answer is No. The is no simple System based method in .NET to get the Home directory such that we could expect an implementation in both .NET on Windows and in Mono.
底线答案是否定的。.NET 中没有简单的基于系统的方法来获取主目录,因此我们可以期望在 Windows 和 Mono 上的 .NET 中实现。
You will need to do some OS detection and branch to OS specific code.
您将需要进行一些操作系统检测并分支到操作系统特定的代码。
回答by James
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Personal is My Documents (or Documents in win7 and above).
个人是我的文档(或在 win7 及以上版本中的文档)。
回答by Nicosmik
This can be done using GetEnvironmentVariable
in System.IO
:
这可以使用GetEnvironmentVariable
in完成System.IO
:
public string GetUserHome() {
var homeDrive = Environment.GetEnvironmentVariable("HOMEDRIVE");
if (!string.IsNullOrWhiteSpace(homeDrive))
{
var homePath = Environment.GetEnvironmentVariable("HOMEPATH");
if (!string.IsNullOrWhiteSpace(homePath))
{
var fullHomePath = homeDrive + Path.DirectorySeparatorChar + homePath;
return Path.Combine(fullHomePath, "myFolder");
}
else
{
throw new Exception("Environment variable error, there is no 'HOMEPATH'");
}
}
else
{
throw new Exception("Environment variable error, there is no 'HOMEDRIVE'");
}
}
Then it produces under windows: C:\\\\Users\\myusername\\myFolder
然后它在windows下产生: C:\\\\Users\\myusername\\myFolder
Note that if you use
请注意,如果您使用
var fullHomePath = Path.Combine(homeDrive.ToString(), homePath.ToString())
it fails because it produces: \\Users\\myusername\\myFolder
它失败是因为它产生: \\Users\\myusername\\myFolder
回答by KBP
In DotNetCore 1.1 System.Environment.SpecialFolder
does not exist. It might exist in 2.0-beta. Until then, to do this you can use the following:
在 DotNetCore 1.1System.Environment.SpecialFolder
中不存在。它可能存在于 2.0-beta 中。在此之前,您可以使用以下方法:
var envHome = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "HOMEPATH" : "HOME";
var home = Environment.GetEnvironmentVariable(envHome);`