C#获取%的应用程序数据%的路径 [英] C# getting the path of %AppData%

查看:241
本文介绍了C#获取%的应用程序数据%的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#2008 SP1

C# 2008 SP1

我用下面的code:

dt.ReadXml("%AppData%\\DateLinks.xml");

不过,我得到一个例外,指向在我的应用程序运行的位置:

However, I am getting an exception that points to the location of where my application is running from:

找不到路径的一部分   D:\项目\ SubVersionProjects \ CatDialer \斌\调试\%APPDATA%\ DateLinks.xml'。

Could not find a part of the path 'D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml'.

我想到了%的AppData%应该找到的相对路径。当我去开始|运行|%APPDATA% Windows资源管理器带我到该目录

I thought the %AppData% should find the relative path. When I go Start|Run|%AppData% windows explorer takes me to that directory.

我不能把完整路径,因为用户是每个客户机上的不同。

I can not put the full path in, as the user is different on each client machine.

非常感谢任何意见,

推荐答案

要获得的应用程序数据的目录中,最好使用 GetFolderPath 方法

To get the AppData directory, it's best to use the GetFolderPath method:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

%的AppData%是一个环境变量,他们不会自动展开任何地方在.net中,虽然可以明确地使用<一个href="http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx"><$c$c>Environment.ExpandEnvironmentVariable方法这样做。我还是强烈建议你使用 GetFolderPath 然而,因为作为约翰内斯·罗塞尔指出,在注释中,%的AppData%可能不会在某些情况下设置的。

%AppData% is an environment variable, and they are not automatically expanded anywhere in .NET, although you can explicitly use the Environment.ExpandEnvironmentVariable method to do so. I would still strongly suggest that you use GetFolderPath however, because as Johannes Rössel points out in the comment, %AppData% may not be set in certain circumstances.

最后,创建如在你的榜样路径:

Finally, to create the path as shown in your example:

var fileName = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), "DateLinks.xml")

这篇关于C#获取%的应用程序数据%的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆