我想保存與web API zip文件,其拋出的異常:在訪問路徑 'C: WINDOWS SYSTEM32 INETSRV dotnetzip-uxoebj5p.tmp' 被拒絕
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(String path,FileMode mode,FileAccess access, FileShare share,Int32 bufferSize,FileOptions options,String msgPath, Boolean在System.IO.FileStream..ctor(String path,FileMode mode)中的bFromProxy at Ionic.Zip.SharedUtilities.CreateAndOpenUniqueTempFile(String dir,Stream & fs,String & filename) 在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;
我也試過
zip.TempFileFolder = Configurations.AppDataPath;
但這似乎並沒有解決問題。
請讓我知道
檢查文件或文件夾是否有權限。 – summerGhost
我無法授予'c:\ windows \ system32 \ inetsrv'權限,但它不允許我在C:\\ Windows –
下提供文件夾權限。必須將文件移動到您擁有足夠權限的位置。 – summerGhost