C# 获取 ASP.NET 临时文件夹路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19129930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get ASP.NET temporary folder path
提问by c00000fd
From my C# code, that doesn't run from within IIS/ASP.NET, I need to add a user account permissions to the ASP.NET temp folder. (It is required while adding my site to IIS.) The folder on my local system is:
从我的 C# 代码中,它不在 IIS/ASP.NET 中运行,我需要向 ASP.NET 临时文件夹添加用户帐户权限。(将我的站点添加到 IIS 时需要它。)我本地系统上的文件夹是:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
I'd hate to hard-code this path into my code, so I was wondering if I can retrieve it from .NET framework itself?
我不想将此路径硬编码到我的代码中,所以我想知道是否可以从 .NET 框架本身检索它?
采纳答案by c00000fd
Hmm. I didn't know that it would be so complicated. For the lack of better answer, I was able to come up with something like this:
唔。我不知道它会如此复杂。由于缺乏更好的答案,我能够想出这样的事情:
using System.Runtime.InteropServices;
string net_base = Path.GetFullPath(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), @"..\.."));
string strTemp32 = string.Concat(net_base, @"\Framework\", RuntimeEnvironment.GetSystemVersion(), @"\Temporary ASP.NET Files");
string strTemp64 = string.Concat(net_base, @"\Framework64\", RuntimeEnvironment.GetSystemVersion(), @"\Temporary ASP.NET Files");
There're obviously two temp folders -- for 32-bit and 64-bit processes. It is based on this sample, and also relies on the assumptionthat default ASP.NET temporary folders are hard-coded
.
显然有两个临时文件夹——用于 32 位和 64 位进程。正是基于这个样本,同时也依赖于假设,即默认的ASP.NET临时文件夹hard-coded
。
Correct me, if you find a better way?
纠正我,如果你找到更好的方法?
回答by Naveed Butt
I think this should help...
我认为这应该会有所帮助...
There is a section in web.config/machine.config
under the compilation tag
where the path is set by default. Here are the attributes of the section...
默认设置路径web.config/machine.config
的compilation tag
位置下面有一个部分。以下是该部分的属性...
回答by Piotr Stapp
Much more safe will be if you use your own temporary folder in for example App_Data
如果您在例如 App_Data 中使用自己的临时文件夹会更安全
Unfortunately Path.GetTempPath();
won't return this folder because it is asp.net internal folder.
不幸的是Path.GetTempPath();
不会返回此文件夹,因为它是 asp.net 内部文件夹。
The good news is that you can change it specifing the file location in web.config with element.
好消息是,您可以使用 element 指定 web.config 中的文件位置来更改它。
回答by VahidN
Try System.Web.HttpRuntime.CodegenDir
to get the physical path of directory where the ASP.NET temporary files are stored for the current application.
尝试System.Web.HttpRuntime.CodegenDir
获取当前应用程序存储 ASP.NET 临时文件的目录的物理路径。
回答by LunielleDev
Simplest way with validation:
最简单的验证方法:
if (file.ContentLength > 0)
{
string temp = Path.GetTempPath();
var path = Path.Combine(temp, fileName);
file.SaveAs(path);
}
and in web.config:
并在 web.config 中:
<system.web>
<compilation tempDirectory="D:\MyTempFiles" />
</system.web>