【问题标题】:ASP.Net MVC2: Where can I store a file to be able to access it on both my dev box and in production?ASP.Net MVC2:我在哪里可以存储文件以便能够在我的开发盒和生产环境中访问它?
【发布时间】:2010-03-05 21:38:11
【问题描述】:
我需要存储一个文件,以便我的 ASP.Net MVC 应用程序可以在我在 Visual Studio 中运行网站时以及在生产服务器实际运行时访问该文件。我不认为我可以在我的开发盒上进行相对路径,因为执行路径位于 System32 文件夹中。我不知道服务器上是否也是如此,但无论哪种方式,绝对路径都不是一种选择。
有没有一种方法可以在代码中引用这个文件,这对我的开发盒和生产服务器都有效?
【问题讨论】:
标签:
asp.net-mvc
file
relative-path
absolute-path
【解决方案1】:
如果您可以将文件存储在应用程序层次结构中,则可以这样做:
Server.MapPath("~/path/to/file")
或者,您可以像这样获取当前正在执行的程序集的路径:
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
并从中构建您的相对路径。
【解决方案2】:
尝试以下子文件夹:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))