2009-08-15 12 views
2

Dans le fichier encryptFile si je change l'instruction if à true, le code fonctionnera comme prévu. Cependant, je reçois des fenêtres de la console sur l'écran qui est moche. Quand je fais fausse FileListName compresser en tant qu'archive vide. Pourquoi?7z et le fichier flush. Cela ne compresse pas mon fichier

 using (TextWriter tw = (TextWriter)new StreamWriter(FileListName)) 
     { 
      writeFilename(tw, t, "."); 
      tw.Flush(); 
      tw.Close(); 
     } 
     encryptFile(FileListName, dst + Path.GetFileName(FileListName)+".7z", password, null); 




    void encryptFile(string srcFile, string dstFile, string pass, int? compressLevel) 
    { 
     string line; 
     var p = new Process(); 
     line = string.Format("a -t7z \"{0}\" \"{1}\" -mhe -p{2} ", dstFile, srcFile, pass); 
     if (compressLevel != null) 
      line += string.Format("-mx{0} ", compressLevel); 
     p.StartInfo.Arguments = line; 
     p.StartInfo.FileName = @"C:\Program Files\7-Zip\7z.exe"; 
     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     if (false) 
     { 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.RedirectStandardError = true; 
      p.Start(); 

      var sr = p.StandardOutput; 
      var err = p.StandardError; 
      Console.Write(sr.ReadToEnd()); 
      Console.Write(err.ReadToEnd()); 
     } 
     else 
      p.Start(); 
    } 

Répondre

1

Pour se débarrasser de la fenêtre, vous devriez essayer

p.StartInfo.CreateNoWindow = true; 
1

Vous devez appeler p.WaitForExit() après p.Start(). Consultez la documentation:

La raison pour laquelle il fonctionne quand vous avez if (true) est que les appels que vous ReadToEnd() forcer efficacement à attendre jusqu'à ce que le processus est sorti de toute façon.

+0

Bam, ça marche. Cela fait parfaitement sens, surtout quand j'écris delete (FileListName) après encryptFile :) –

+0

Cool, heureux que cela a fonctionné! :) – ars