ASP.Net MVC2:我可以在哪里存储文件以便能够在我的开发盒和生产环境中访问它?

发布于 2024-08-24 05:37:33 字数 204 浏览 1 评论 0原文

我需要存储一个文件,以便我的 ASP.Net MVC 应用程序可以在 Visual Studio 中运行网站时以及生产服务器实际运行时访问该文件。我不认为我可以在我的开发盒上执行相对路径,因为执行路径位于 System32 文件夹中。我不知道服务器上是否也是如此,但无论哪种方式,绝对路径都不是一个选项。

有没有一种方法可以在代码中引用此文件,并且适用于我的开发盒和生产服务器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-08-31 05:37:33

如果您可以将文件存储在应用程序层次结构中,则可以执行以下操作:

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

并从中构造相对路径。

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.

零時差 2024-08-31 05:37:33

尝试以下子文件夹:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))

Try a subfolder of the:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文