C# 访问路径';c:\windows\system32\inetsrv\dotnetzip-uxoebj5p.tmp';被拒绝
我试图用web api保存一个zip文件,该文件引发异常:C# 访问路径';c:\windows\system32\inetsrv\dotnetzip-uxoebj5p.tmp';被拒绝,c#,asp.net-web-api,C#,Asp.net Web Api,我试图用web api保存一个zip文件,该文件引发异常: Access to the path 'c:\windows\system32\inetsrv\DotNetZip-uxoebj5p.tmp' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileA
Access to the path 'c:\windows\system32\inetsrv\DotNetZip-uxoebj5p.tmp' is
denied. at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy, Boolean useLongPath, Boolean checkHost)
在System.IO.FileStream..ctor(字符串路径、文件模式、文件访问、,
FileShare共享、Int32 bufferSize、FileOptions选项、字符串msgPath、,
System.IO.FileStream..ctor上的布尔bFromProxy(字符串路径,文件模式)
在Ionic.Zip.SharedUtilities.CreateAndOpenUniqueTempFile(字符串目录、流和
fs、字符串和文件名)
在Ionic.Zip.ZipFile.get_WriteStream()上
在Ionic.Zip.ZipFile.Save()处
在Archnies.Archnies.DownloadFile(字符串url)
代码:
Logger.LogMessage("Downloading File From URL " + url);
// Construct HTTP request to get the file
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.CookieContainer = new System.Net.CookieContainer();
for (int i = 0; i <= driver.Manage().Cookies.AllCookies.Count - 1; i++)
{
System.Net.Cookie ck = new System.Net.Cookie(driver.Manage().Cookies.AllCookies[i].Name, driver.Manage().Cookies.AllCookies[i].Value, driver.Manage().Cookies.AllCookies[i].Path, driver.Manage().Cookies.AllCookies[i].Domain);
httpRequest.CookieContainer.Add(ck);
}
String userAgent = (String)((IJavaScriptExecutor)driver).ExecuteScript("return navigator.userAgent;");
httpRequest.Accept = "text/html, application/xhtml+xml, */*";
httpRequest.UserAgent = userAgent;// "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
//HttpStatusCode responseStatus;
// Get back the HTTP response for web server
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
// Define buffer and buffer size
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
// Read from response and write to file
string userProfile = Configurations.AppDataPath; //Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
byte[] b = null;
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
//count = stream.Read(buf, 0, 1024);
count = httpResponseStream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (httpResponseStream.CanRead && count > 0);//while (stream.CanRead && count > 0);
b = ms.ToArray();
}
string fileName = Path.Combine(userProfile, String.Format("archaniesReport{0}.zip", new Random().Next(512365412)));
Logger.LogMessage("File: " + fileName);
ZipFile zip = new ZipFile();
zip.AddEntry(fileName, b);
zip.Save(Path.GetFileName(fileName));
zip.Dispose();
using (FileStream fileStream = File.Create(fileName))
{
while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
File.Copy(Path.GetFileName(fileName), fileName, true);
return true;
但这似乎并不能解决问题
请告诉我您可以将文件下载到临时位置,您可以使用
Path.GetTempPath()
()获得该位置。下载完成后,您可以通过文件移动文件。将(…)
复制到用户文件夹,并通过文件从临时位置删除文件。删除(…)
使用这种方法,您不会遇到访问被拒绝的异常。您可以将文件下载到临时位置,您可以使用
Path.GetTempPath()
()获得该位置。下载完成后,您可以通过文件将文件移动。将(…)
复制到用户文件夹,并通过文件从临时位置删除文件。删除(…)
使用此方法,您不会遇到拒绝访问的异常。检查文件或文件夹是否具有权限。我无法授予“c:\windows\system32\inetsrv”权限,它不允许我在c:\\windows下授予文件夹权限。您必须将文件移到您有足够权限的外部。检查文件或文件夹是否具有权限。我无法授予perm“c:\windows\system32\inetsrv”的权限,它不允许我在c:\\windows下授予文件夹权限。您必须将文件移到您有足够权限的外部。我正在尝试将文件保存在IIS目录的AppData中,该目录具有完全权限,我相信dotnetzip库安装在windows目录下,该目录为IIS服务器无法访问,因此引发异常。解决方案是什么?因此
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
是正确的解决方案吗?我正在尝试将文件保存在IIS目录的AppData中,该目录具有完全权限,我相信dotnetzip库安装在IIS服务器无法访问的windows目录上,因此引发异常。解决方案是什么?因此Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
是正确的解决方案吗?
zip.TempFileFolder = Configurations.AppDataPath;