2017-08-03 2 views
-1

J'ai essayé de compresser les fichiers dans le dossier "export" qui est inclus dans mon projet. Mais System.UnauthorizedAccessException retourné, il dit accès refusé. Comment puis-je atteindre le fichier d'exportation?System.UnauthorizedAccessException, comment accéder à ces fichiers?

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

     ZipFile.CreateFromDirectory(startPath, zipPath); 

     ZipFile.ExtractToDirectory(zipPath, extractPath); 
    } 

Et j'ai appelé cette fonction de contrôleur comme ceci:

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

Ce bloc est où je crée les fichiers. J'ai sérialisé les tables DB et les enregistrer en tant que fichiers JSON en utilisant Newtonsoft. Et la dernière chose est de compresser ces fichiers json dans un fichier 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); 
     } 

Je suppose que je dois faire quelques changements dans la partie ci-dessus, mais je ne peux pas le savoir.

Répondre

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

J'ai ajouté ces lignes avant JsonSerializer et cela a fonctionné.