我如何获取当前用户的“应用程序数据”的路径 文件夹?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看看结合 Environment.GetFolderPath 和 Environment.SpecialFolder 来执行此操作。
Look at combining Environment.GetFolderPath and Environment.SpecialFolder to do this.
根据您正在执行的操作,您可能还需要查看“
如果用户位于域中,它将仅存储在其本地
AppData
”文件夹中,并且不会与其漫游配置文件同步。Depending on what you are doing you might also want to look at
If the user is on a domain it will only be stored in their local
AppData
folder and not synced with their roaming profile.查看
Environment.SpecialFolders
这也应该能让您满足用户名要求。
Have a look at the
Environment.SpecialFolders
that should get you round the username requirement as well.
查看 System.Environment 类及其属性和方法,例如:
第一个返回“C:\Windows\system32”,第二个返回“C:\Documents”和设置\用户名\我的文档”。
Have a look at the System.Environment class and its properties and methods, e.g:
The first one returns "C:\Windows\system32" for example and the second one "C:\Documents and Settings\USERNAME\My Documents".
尝试这个:
Try this:
您实际上不需要获取系统驱动器的值或当前登录的用户名来实现此目的。 有预定义的环境变量
%localAppData%
和%appData%
,它们为您提供这些目录的完全限定路径,如下面的代码所示:同样,您不需要用户名来获取应用程序数据路径,正如我上面讨论的那样。不过,为了了解知识,您可以从
%username%
环境变量中获取它,如下所示如下图所示: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: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: