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

时间:2022-10-01 20:47:31

1)how can i find out the Windows Installation drive in which the user is working.? I need this to navigate to the ApplicationData in DocumentsandSettings.

1)如何找到用户正在使用的Windows安装驱动器。我需要这个导航到DocumentsandSettings中的ApplicationData。

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

6 个解决方案

#1


Look at combining Environment.GetFolderPath and Environment.SpecialFolder to do this.

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

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

#2


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

#3


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.

这应该可以让你绕过用户名要求。

#4


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”。

#5


Try this:

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

#6


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)我怎样才能获得用户名,以便我可以转到ApplicaitionData。例如:“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%");

#1


Look at combining Environment.GetFolderPath and Environment.SpecialFolder to do this.

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

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

#2


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

#3


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.

这应该可以让你绕过用户名要求。

#4


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”。

#5


Try this:

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

#6


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)我怎样才能获得用户名,以便我可以转到ApplicaitionData。例如:“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%");