views:

179

answers:

2

I need to store a file, such that my ASP.Net MVC app can access the file, both when I run the website in visual studio, and when the production server is actually running. I don't think that I can do relative pathing on my dev box, because the execution path is something in the System32 folder. I don't know if the same is true on the server, but either way, an absolute path is not an option.

Is there a way that I can refer to this file in code, that will work for both my dev box and my production server?

+2  A: 

Try a subfolder of the:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
GvS
+2  A: 

If you can store the file within your application hierarchy, you can do this:

Server.MapPath("~/path/to/file")

Alternatively, you can get the path of the currently executing assembly like so:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);

And construct your relative path from that.

Gabe Moothart