简体   繁体   English

C#运行后停止进程

[英]c# stop process after running

I have the problem that I want to unzip a zip file with 7za.exe and after that I want to kill the process. 我有一个问题,我想用7za.exe解压缩一个zip文件,然后我要终止该进程。 If I am using code like in the following, I have the problem that the process got killed before the process unzips the file. 如果我使用下面的代码,则会遇到在进程解压缩文件之前该进程被杀死的问题。

I have already tried to use HasExited or WaitForExit, but no chance. 我已经尝试使用HasExited或WaitForExit,但是没有机会。 Has anyone an idea? 有人知道吗? The only working way is to use a Thread.Sleep(1000) function, which I can't use because it slows some other processes. 唯一的工作方法是使用Thread.Sleep(1000)函数,因为它会使某些其他进程变慢,所以我无法使用它。

System.Diagnostics.ProcessStartInfo zipper = new System.Diagnostics.ProcessStartInfo(@"##archzip.text##");
            zipper.Arguments = string.Format("x " + (PathOfFile) + "  -o" + (NewPathOfFile) + "");
            zipper.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(zipper);
            proc.Kill();

Thanks in advance! 提前致谢!

You can try the code below 您可以尝试下面的代码

using System.IO.Compression;

public static void Decompress(FileInfo fileToDecompress)
{
    using (FileStream originalFileStream = fileToDecompress.OpenRead())
    {
        string currentFileName = fileToDecompress.FullName;
        string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

        using (FileStream decompressedFileStream = File.Create(newFileName))
        {
            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(decompressedFileStream);
                Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
            }
        }
        }
}

If you want to decompress a folder, you can use the command line below: 如果要解压缩文件夹,可以使用以下命令行:

ZipFile.ExtractToDirectory(@"C:\temp\file\9906759_Testpraefix_14028604.zip", @"C:\temp\file\extract\9906759_Testpraefix_14028604");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM