2016-05-20 6 views
0

Ich habe einen Dateiextraktor erstellt, jetzt funktioniert es, aber es bewegt auch alle Dateien von der startDir zu der destDir zusammen mit der Zip-Datei. Wie kann ich dieses Programm veranlassen, nur die Zip-Datei zu verschieben, anstatt alle Dateien?Datei Extractor bringt alle Dateien statt nur Zip-Datei

Quelle:

using System; 
using System.IO.Compression; 

namespace ArchiveCreator 
{ 
    class Program 
    { 
     //When program is run successfully this will be the output 
     public string Success(string input) 
     { 
      Console.ForegroundColor = ConsoleColor.White; 
      Console.WriteLine(input); 
      return input; 
     } 

     //When program encounters an error this will be the output 
     public string Warn(string input) 
     { 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine(input); 
      return input; 
     } 

     //When program has information to show, this will be the output 
     public string Say(string input) 
     { 
      Console.ForegroundColor = ConsoleColor.DarkCyan; 
      Console.WriteLine(input); 
      return input; 
     } 

     //Main method 
     static void Main(string[] args) 
     { 
      //These variables are used to create a 
      //random string that will be used as the 
      //zip files name 
      var chars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
      var stringChars = new char[8]; 
      var random = new Random(); 

      //Info is used as provide the type of 
      //information that will be displayed 
      //by the program 
      Program info = new Program(); 

      //Create the zip file name 
      for (int i = 0; i < stringChars.Length; i++) 
      { 
       stringChars[i] = chars[random.Next(chars.Length)]; 
      } 
      string finalString = new String(stringChars); 

      info.Say("Starting file extraction.."); 

      string startDir = @"c:/users/thomas_j_perkins/test_folder"; 
      string destDir = @"c:/users/thomas_j_perkins/archive/"; 
      string zipName = $"c:/users/thomas_j_perkins/archive/{finalString}.zip"; 

      try 
      { 
       ZipFile.CreateFromDirectory(startDir, zipName); 
       ZipFile.ExtractToDirectory(zipName, destDir); 
      } 
      catch (Exception e) 
      { 
       info.Warn($"Error: {e}"); 
      } 
      info.Success($"Extracted files successfully to: {destDir}"); 
      info.Say("Press enter to exit.."); 
      Console.ReadLine(); 
     } 
    } 
} 

Bild des Verzeichnisses nach Programm ausgeführt wird: enter image description here

Antwort

1

Ihr Code wird im Zielverzeichnis eine ZIP-Datei zu erstellen, wenn Sie

ZipFile.CreateFromDirectory(startDir, zipName); 

Die zipname nennen Pfad ist im Zielverzeichnis. Hast du vor, es in den startDir zu setzen?

Zeichenfolge startDir = @ "c:/users/thomas_j_perkins/test_folder";

Zeichenfolge destDir = @ "c:/users/thomas_j_perkins/archive/";

String zipName = $ "c:/users/thomas_j_perkins/archive/ {finalString} .zip";

+0

Ich verstehe nicht, was du meinst? – 13aal

+0

Wenn Sie CreateFromDirectory aufrufen, fügen Sie die Zip-Datei Ihrem Zielordner hinzu. Dann extrahieren Sie alle Dateien in den gleichen Ordner, so dass die Zip immer noch mit den Dateien da ist. Extract löscht den Zip nach der Extraktion nicht. – bhmahler

+0

OOOOOH Ich verstehe, danke, Sir! – 13aal

Verwandte Themen