tags:

views:

5957

answers:

5

Hello,

C# 2008 SP1

I am using this code below:

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

However, I am getting an exception that point 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.

Many thanks for any advice,

+39  A: 

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

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

%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")
Noldorin
+1 for offering a real solution, not relying on the environment. To add to the answer: Not every function that handles file names expands environment variables. In fact, usually you have to explicitly do this, otherwise it doesn't work and you'll end up with %something% folders. Furthermore, the environment does not need to be present, in some cases when running a program under another user account the user's environment will not be loaded and %Appdata% will be empty. That's why you would want to use the documented APIs for getting those folders (unless you're using batch files, though).
Joey
@Johannes: Good info there. I just amended my answer as you posted that, but I'll make it clearer that GetFolderPath is definitely preferable over ExpandEnvironmentVariable.
Noldorin
+1 for Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), I was behind this for couple of days now.
Sumit Ghosh
+1  A: 

I don't think putting %AppData% in a string like that will work.

try

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString()
danswain
+1  A: 

I think you can also use Environment.ExpandEnvironmentVariables("%AppData%\DateLinks.xml"); to expand the %AppData% variable.

parapet
+1  A: 

The path is different if you're talking ASP.NET.

I couldn't find any of the 'SpecialFolder' values that pointed to /App_Data for ASP.NET.

Instead you need to do this:

 HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data")

(Note: You don't need the 'Current' property in an MVC Controller)

If theres another more 'abstract' way to get to App_Data would love to hear how.

Simon_Weaver
+2  A: 

In .net2.0 you can use the variable Application.UserAppDataPath

Nathan Reed