C#获取%的应用程序数据%的路径应用程序、路径、数据

由网友(抓紧光芒不放手i)分享简介:C#2008 SP1 C# 2008 SP1我用下面的code:dt.ReadXml("%AppData%\\DateLinks.xml");不过,我得到一个例外,指向在我的应用程序运行的位置:However, I am getting an exception that points to the loca...

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:ProjectsSubVersionProjectsCatDialerbinDebug%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中,虽然可以明确地使用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")
阅读全文

相关推荐

最新文章