2

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

2 回答 2

1

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

它起作用的原因if (true)ReadToEnd()调用有效地迫使你等到进程退出。

于 2009-08-15T12:13:10.580 回答
1

要摆脱窗口,您应该尝试

p.StartInfo.CreateNoWindow = true;
于 2009-08-15T10:47:12.547 回答