ASP.Net MVC2: Where can I store a file to be able to access it on both my dev box and in production?

StackOverflow https://stackoverflow.com/questions/2390109

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

Try a subfolder of the:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top