patterncsharpCritical
C# getting the path of %AppData%
Viewed 0 times
pathappdatagettingthe
Problem
C# 2008 SP1
I am using the code below:
However, I am getting an exception that points to the location of where my application is running from:
Could not find a part of the path
'D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml'.
I thought the
I can not put the full path in, as the user is different on each client machine.
I am using the code below:
dt.ReadXml("%AppData%\\DateLinks.xml");However, I am getting an exception that points to the location of where my application is running from:
Could not find a part of the path
'D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml'.
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.
Solution
To get the AppData directory, it's best to use the
(must add
Finally, to create the path as shown in your example:
GetFolderPath method:Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)(must add
using System if not present).%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");Code Snippets
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)var fileName = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "DateLinks.xml");Context
Stack Overflow Q#867485, score: 1019
Revisions (0)
No revisions yet.