访问路径&#c; \ windows \ system32 \ inetsrv \ dotnetzip-uxoebj5p.tmp'被拒绝

时间:2017-07-25 11:23:24

标签: 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, 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(字符串路径,FileMode模式,FileAccess访问,    FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,    System.IO.FileStream..ctor中的布尔bFromProxy(字符串路径,FileMode模式)     在Ionic.Zip.SharedUtilities.CreateAndOpenUniqueTempFile(String dir,Stream&     fs,String&文件名)     在Ionic.Zip.ZipFile.get_WriteStream()     在Ionic.Zip.ZipFile.Save()     在Archnies.Archnies.DownloadFile(String 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;

但这似乎无法解决问题。

请告诉我

1 个答案:

答案 0 :(得分:0)

您可以将文件下载到临时位置,使用Path.GetTempPath()MSDN)。下载完成后,您可以按File.Copy(...)将文件移动到用户的文件夹,并通过File.Delete(...)从temp中删除文件。

使用此方法,您将无法获得拒绝访问权限。