2017-07-25 109 views
0

我想保存與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; 

但這似乎並沒有解決問題。

請讓我知道

+0

檢查文件或文件夾是否有權限。 – summerGhost

+0

我無法授予'c:\ windows \ system32 \ inetsrv'權限,但它不允許我在C:\\ Windows –

+0

下提供文件夾權限。必須將文件移動到您擁有足夠權限的位置。 – summerGhost

回答

0

您可以將文件下載到臨時位置,你開始使用Path.GetTempPath()MSDN)。下載完成後,您可以將文件File.Copy(...)移動到用戶的文件夾並從File.Delete(...)刪除文件。

使用這種方法你不會得到拒絕異常的訪問。

+0

我試圖將文件保存在IIS目錄的AppData中,它具有完整的權限,我相信dotnetzip libarary安裝在windows目錄中,哪個IIS服務器無法訪問,因此引發異常 什麼是可以的解決方案? –

+0

那麼'Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)'是否正確的解決方案? –