user34537
user34537

Reputation:

7z and file flush. Its not compressing my file

In encryptFile file if i change the if statement to true the code will work as expected. However i get console windows on screen which is ugly. When i make it false FileListName compress as empty archive. Why?

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

Upvotes: 2

Views: 294

Answers (2)

ars
ars

Reputation: 123518

You need to call p.WaitForExit() after p.Start(). See the documentation:

The reason it works when you have if (true) is that the ReadToEnd() calls effectively force you to wait until the process has exited anyway.

Upvotes: 1

Peter Lillevold
Peter Lillevold

Reputation: 33930

To get rid of the window you should try

p.StartInfo.CreateNoWindow = true;

Upvotes: 1

Related Questions