简体   繁体   English

如何获取当前用户的“Application Data”文件夹的路径?

[英]How can i get the path of the current user's “Application Data” folder?

1)how can i find out the Windows Installation drive in which the user is working.? 1)如何找到用户正在使用的Windows安装驱动器。 I need this to navigate to the ApplicationData in DocumentsandSettings. 我需要这个导航到DocumentsandSettings中的ApplicationData

2)Also how can i get the user name too so that i can goto ApplicaitionData.? 2)我怎样才能获得用户名,以便我可以转到ApplicaitionData。 Eg: "D:\\Documents and Settings\\user\\Application Data". 例如:“D:\\ Documents and Settings \\ user \\ Application Data”。

看看组合Environment.GetFolderPathEnvironment.SpecialFolder来做到这一点。

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

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文件夹中,而不会与其漫游配置文件同步。

Have a look at the Environment.SpecialFolders 看看Environment.SpecialFolders

Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System

that should get you round the username requirement as well. 这应该可以让你绕过用户名要求。

Have a look at the System.Environment class and its properties and methods, eg: 查看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 ”。

尝试这个:

string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);

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.? 2)我怎样才能获得用户名,以便我可以转到ApplicaitionData。 Eg: "D:\\Documents and Settings\\user\\Application Data". 例如:“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%");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获取当前用户的Local Settings文件夹路径? - How do I get the current user's Local Settings folder path in C#? 如何将文件放入安装项目中用户的本地应用程序数据文件夹中? - How can I put files in the user's local application data folder in a setup project? 如何在 .NET 控制台应用程序中获取应用程序的路径? - How can I get the application's path in a .NET console application? 如何获取当前用户的我的应用程序设置文件夹路径? - How to get my app settings folder path for the current user? 如何以编程方式获取另一个应用程序的安装路径? - How can I get another application's installation path programmatically? 获取当前登录用户的Documents文件夹路径 - Get Documents folder path of current logged on user 作为系统帐户运行的 Windows 服务如何获取当前用户的 \AppData\Local\ 特殊文件夹? - How can Windows service running as System account get current user's \AppData\Local\ special-folder? C#如何获取当前文件夹路径的所有子文件夹 - C# How can I get all of subfolder of the current folder path 如何获取应用程序快捷方式的当前目录路径 - How to get the current directory path of application's shortcut 如何在C#中以编程方式获取“ Application Data”文件夹路径? - How to get “Application Data” folder path programmatically in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM