C# 如何获取当前用户的“应用程序数据”文件夹的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915210/
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
How can i get the path of the current user's "Application Data" folder?
提问by SyncMaster
1)how can i find out the Windows Installation drive in which the user is working.? I need this to navigate to the ApplicationDatain DocumentsandSettings.
1) 我怎样才能找到用户正在工作的 Windows 安装驱动器?我需要它来导航到DocumentsandSettings 中的ApplicationData。
2)Also how can i get the user nametoo so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".
2)另外,我怎样才能获得用户名,以便我可以转到应用程序数据。?例如:“D:\Documents and Settings\user\Application Data”。
采纳答案by Martin Harris
Look at combining Environment.GetFolderPathand Environment.SpecialFolderto do this.
看看结合Environment.GetFolderPath和Environment.SpecialFolder来做到这一点。
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
回答by Eoin Campbell
Have a look at the Environment.SpecialFolders
Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System
that should get you round the username requirement as well.
这也应该让您绕过用户名要求。
回答by Magnus Johansson
Try this:
尝试这个:
string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
回答by M4N
Have a look at the System.Environment class and its properties and methods, e.g:
查看 System.Environment 类及其属性和方法,例如:
string systemDir = System.Environment.SystemDirectory;
string docs = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.MyDocuments));
string systemDrive = System.IO.Path.GetPathRoot(systemDir);
The first one returns "C:\Windows\system32" for example and the second one "C:\Documents and Settings\USERNAME\My Documents".
例如,第一个返回“ C:\Windows\system32”,第二个返回“ C:\Documents and Settings\USERNAME\My Documents”。
回答by Twelve47
Depending on what you are doing you might also want to look at
根据您在做什么,您可能还想查看
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
If the user is on a domain it will only be stored in their local AppData
folder and not synced with their roaming profile.
如果用户在域中,它只会存储在他们的本地AppData
文件夹中,而不会与他们的漫游配置文件同步。
回答by RBT
1)how can i find out the Windows Installation drive in which the user is working.?
1) 我怎样才能找到用户正在工作的 Windows 安装驱动器?
var systemDrive = Environment.ExpandEnvironmentVariables("%systemdrive%");
I need this to navigate to the ApplicationData in DocumentsandSettings.
我需要它来导航到 DocumentsandSettings 中的 ApplicationData。
You don't really require to fetch the value of either system drive or currently logged in user name to achieve this. There are predefined environment variables %localAppData%
and %appData%
which give you fully qualified path of these directories as shown in the code below:
您实际上不需要获取系统驱动器或当前登录的用户名的值来实现此目的。有预定义的环境变量%localAppData%
,%appData%
它们为您提供这些目录的完全限定路径,如下面的代码所示:
var localApplicationData = Environment.ExpandEnvironmentVariables("%localappdata%");
//this gives C:\Users\<userName>\AppData\Local
var roamingApplicationData = Environment.ExpandEnvironmentVariables("%appdata%");
//this gives C:\Users\<userName>\AppData\Roaming
2)Also how can i get the user name too so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".
2)另外,我怎样才能获得用户名,以便我可以转到应用程序数据。?例如:“D:\Documents and Settings\user\Application Data”。
Again, you don't need user name to get the application data path as I've discussed above. Still, for the sake of knowledge you can fetch it from %username%
environment variable as shown below:
同样,您不需要用户名来获取应用程序数据路径,就像我上面讨论的那样。不过,为了知识起见,您可以从%username%
环境变量中获取它,如下所示:
var currentUserName = Environment.ExpandEnvironmentVariables("%username%");