我如何获取当前用户的“应用程序数据”的路径 文件夹?

发布于 2024-07-22 06:46:03 字数 227 浏览 7 评论 0原文

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

2)另外,我怎样才能获得用户名,以便我可以转到ApplicaitionData。? 例如:“D:\Documents and Settings\user\Application Data”。

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.

2)Also how can i get the user name too so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一花一树开 2024-07-29 06:46:03

看看结合 Environment.GetFolderPathEnvironment.SpecialFolder 来执行此操作。

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

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

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
苦妄 2024-07-29 06:46:03

根据您正在执行的操作,您可能还需要查看“

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

如果用户位于域中,它将仅存储在其本地 AppData”文件夹中,并且不会与其漫游配置文件同步。

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.

小嗲 2024-07-29 06:46:03

查看 Environment.SpecialFolders

Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System

这也应该能让您满足用户名要求。

Have a look at the Environment.SpecialFolders

Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System

that should get you round the username requirement as well.

羁客 2024-07-29 06:46:03

查看 System.Environment 类及其属性和方法,例如:

string systemDir = System.Environment.SystemDirectory;
string docs = System.Environment.GetFolderPath(
    System.Environment.SpecialFolder.MyDocuments));

string systemDrive = System.IO.Path.GetPathRoot(systemDir);

第一个返回“C:\Windows\system32”,第二个返回“C:\Documents”和设置\用户名\我的文档”。

Have a look at the System.Environment class and its properties and methods, e.g:

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".

枫以 2024-07-29 06:46:03

尝试这个:

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

Try this:

string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
乜一 2024-07-29 06:46:03

1)如何找到用户所在的Windows安装驱动器
工作中。?

    var systemDrive =  Environment.ExpandEnvironmentVariables("%systemdrive%");

我需要它来导航到应用程序数据
文档和设置。

您实际上不需要获取系统驱动器的值或当前登录的用户名来实现此目的。 有预定义的环境变量 %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)另外我怎样才能获得用户名以便我可以转到
应用程序数据。? 例如:“D:\Documents and Settings\user\Application
数据”。

同样,您不需要用户名来获取应用程序数据路径,正如我上面讨论的那样。不过,为了了解知识,您可以从 %username% 环境变量中获取它,如下所示如下图所示:

    var currentUserName = Environment.ExpandEnvironmentVariables("%username%");

1)how can i find out the Windows Installation drive in which the user
is working.?

    var systemDrive =  Environment.ExpandEnvironmentVariables("%systemdrive%");

I need this to navigate to the ApplicationData in
DocumentsandSettings.

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:

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".

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:

    var currentUserName = Environment.ExpandEnvironmentVariables("%username%");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文