2017-08-03 55 views
-1

我試圖壓縮包含在我的項目中的「導出」文件夾中的文件。但System.UnauthorizedAccessException返回,它說訪問被拒絕。我怎樣才能達到出口文件?System.UnauthorizedAccessException,如何訪問這些文件?

public static void ZipHelper(string path) 
    { 
     string startPath = path; 
     string zipPath = path; 
     string extractPath = path; 

     ZipFile.CreateFromDirectory(startPath, zipPath); 

     ZipFile.ExtractToDirectory(zipPath, extractPath); 
    } 

我命由控制器像這樣的功能:

 Helpers.ZipArchiveHelper.ZipHelper(Server.MapPath("~/export ")); 

此塊是我創建的文件。我使用Newtonsoft序列化數據庫表並將它們保存爲JSON文件。最後一件事是將這些json文件壓縮成一個zip文件。

 name = "componentType.json"; 
     if (!System.IO.File.Exists(Server.MapPath("~/export") + "\\" + name)) 
     { 
      JsonSerializer serializer = new JsonSerializer(); 

      var data = JsonConvert.SerializeObject(compTypeList, Formatting.Indented); 
      System.IO.File.WriteAllText(Server.MapPath("~/export ") + "\\" + name, data); 
     } 
     using (StreamReader sr = new StreamReader(Server.MapPath("~/export ") + "\\" + name)) 
     { 
      var jsonObj = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<ComponentTypeModel>)); 
      MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd())); 
      compTypeList = (List<ComponentTypeModel>)jsonObj.ReadObject(stream); 
     } 

我想我需要在上面的部分進行一些更改,但無法找到。

回答

0
 System.IO.File.SetAttributes(Server.MapPath("~/export"), 
        (new FileInfo(Server.MapPath("~/export"))).Attributes | FileAttributes.Normal); 

我在JsonSerializer之前添加了這些行,它工作。

相關問題