.net 7z和文件刷新。它没有压缩我的文件

.net 7z和文件刷新。它没有压缩我的文件,.net,shell,process,7zip,.net,Shell,Process,7zip,在encryptFile中,如果我将if语句更改为true,代码将按预期工作。然而,我在屏幕上看到的控制台窗口很难看。当我设置为false时,FileListName压缩为空存档。为什么? using (TextWriter tw = (TextWriter)new StreamWriter( FileListName)) { writeFilename(tw, t, "."); tw.Flush();

在encryptFile中,如果我将if语句更改为true,代码将按预期工作。然而,我在屏幕上看到的控制台窗口很难看。当我设置为false时,FileListName压缩为空存档。为什么?

        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();
    }

要想摆脱窗户,你应该试试看

p.StartInfo.CreateNoWindow = true;

您需要在
p.Start()
之后调用
p.WaitForExit()
。请参阅文档:


当您有
if(true)
时,它工作的原因是
ReadToEnd()
调用有效地迫使您等待进程退出。

Bam,它工作。这很有道理,特别是当我在encryptFile:)之后写delete(FileListName)时