views:

193

answers:

2

This question is also related to my other question ASP.NET MVC - Website Paths.

I have a 3rd party component which has hard-coded directory paths compiled into the assembly. An example of one of the paths is:

%APPDATA%\Vendor\Product\settings.ini

So if I create a console application/windows service and run it under my account on my machine (under windows server 2003) and save out these settings they are saved to say this directory:

C:\Documents and Settings\James\Application Data\Vendor\Product\settings.ini

However, if I try to do the same thing via my web app, it doesn't.

I am trying to figure out where the %APPDATA% environment variable is mapped to when used within an ASP.NET MVC web application. I have tried placing the Vendor\Product\ directory in my App_Data directory as suggested by someone in the other question....this didn't work.

I thought perhaps the save would just fail, however, no exception is being thrown the line is processed like it normally would.

+1  A: 

I assume it will be using the environment variables setup for the service running ASP.Net, or the AppPool.

Have you tried setting the environment variable in your application start before you use the component?

Colin Newell
@Colin, no actually good point. I will try this and get back to you. Well the AppPool is running under my account and it is not writing out (as this is what I thought would happen).
James
A: 

FiguringOutAEnvironmentVariableContent

Hi James,

Noldorin published a nice SOLUTION a while ago.

Citing the author...

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")

Hope it helps you.

BONUS: <%= System.Environment.GetEnvironmentVariable("MyVariable") %>

SDReyes
@SDReyes, as the code for saving/reading the settings file is compiled within the DLL I am unaware as to what method it is using to resolve the file path. I would assume it would be using something similar. This, however, is not the issue. It is where the path is resolving to if you do the above whilst in an ASP.NET website.
James
Hi James!test this...<%= System.Environment.GetEnvironmentVariable("APPDATA") %>Hope it helps : )
SDReyes
@SDReyes, nothing gets displayed.
James
Hummm so %APPDATA% is not setted... : /Have you tried to use different accounts for your application pool? As LocalService, LocalSystem... (take care of security!)-BTW are you testing your app over IIS, Cassini...??
SDReyes