-1

So, I am using Execute Shell in a program I am working with and I need to zip some files by command, I decided to use 7zip as it comes with NuGet. I also added a message for the final user saying that the process got completed, I got that message 30 seconds before the process actually completes, so after the message 30 seconds must passed for me to be able to open/access the zip file.

is there any way to keep a track of it or display the message until the process is completed? tried with threads, if statements, whiles, do while, and some more but still no luck, this is how some of the code looks for the zipping part

  public static void ZipFiles()
    {
        ExecuteShell ES = new ExecuteShell();
        ES.ExecuteCommand("cmd.exe", zipCmdText, false, false, 
        "C:\\Windows\\System32", "");           
    }

   and the execute shell part:

public bool ExecuteCommand(string EXEName, string Command, bool 
blnCallWaitForExit, bool blnBatchFile, string strWorkingDirectory, string 
ExecutionDoneMessage)
{
    bool blnCommandExited = false;
    try
    {
        KillAllCommandExe(EXEName);


        List<string> OutPut = new List<string>();

        objProcess = new Process();

        objProcess.OutputDataReceived += new DataReceivedEventHandler(objProcess_OutputDataReceived);
        objProcess.ErrorDataReceived += new DataReceivedEventHandler(objProcess_ErrorDataReceived);
        objProcess.Exited += new EventHandler(objProcess_Exited);
        objProcess.StartInfo.FileName = EXEName;
        if(!blnBatchFile)
            objProcess.StartInfo.Arguments = Command;

        objProcess.StartInfo.UseShellExecute = false;
        if (strWorkingDirectory != "")
            objProcess.StartInfo.WorkingDirectory = strWorkingDirectory;


        objProcess.StartInfo.RedirectStandardInput = true;
        objProcess.StartInfo.RedirectStandardOutput = true;
        objProcess.StartInfo.RedirectStandardError = true;
        objProcess.StartInfo.CreateNoWindow = true;
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        objProcess.Start();

        objProcess.BeginOutputReadLine();
        objProcess.BeginErrorReadLine();

        if (blnCallWaitForExit == true)
        {
            objProcess.WaitForExit(9000);
        }


        if (objProcess.HasExited)
        {
            CloseProcess();
        }
        blnCommandExited = true;
    }
    catch (Exception ex)
    {

    }
    return blnCommandExited;
}

Are there any other better/faster zipping options that could achieve this result?

Any suggestion will be greatly appreciated.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39

1 Answers1

1

7zip is fine but as you noticed it takes some plumbing to work with it as it is an external tool. Instead you could use System.IO.Compression.ZipFile or something else from System.IO.Compression namespace. This will give you better integration with your UI. It is available since .NET Framework 4.5

Maciej
  • 7,871
  • 1
  • 31
  • 36
  • yeah, that would probably be the best path, do you know if it can be used with with the CMD prompt too? – Daniel Godoy Apr 19 '18 at 14:07
  • I am unsure what you mean. `ZipFile` with cmd prompt? – Maciej Apr 19 '18 at 14:14
  • So, all of this has to be done via commands, so with 7zip I open the command prompt from other method and call: /c 7z a -t7z C:\\Users\\dg15335\\Escrow\\XYZCompany\\test.7z C:\\Users\\dg15335\\Escrow\\TFS I wanted to know if something similar can be done with the IO compression, since it's: "ZipFile.CreateFromDirectory(startPath, zipPath);" – Daniel Godoy Apr 19 '18 at 14:40
  • No command prompt parameters for this solution. You set everything up with parameters and properties of objects and classes. It much easier to control and manage. It's also easier to control when zipping finishes and you get better error control etc. – Maciej Apr 23 '18 at 10:19