C# 隐藏复制窗口后台工作程序

C# 隐藏复制窗口后台工作程序,c#,backgroundworker,C#,Backgroundworker,我修改了中的代码,制作了一个压缩和复制目录的程序。我在代码中包含了一个后台工作程序,因此我可以异步运行压缩例程,以防止接口冻结。以下是我改编的代码: private void StartZipping() { var zipWorker = new BackgroundWorker { WorkerReportsProgress = true }; zipWorker.DoWork += ZipWorkerWork; zipWorker.RunWorkerComplet

我修改了中的代码,制作了一个压缩和复制目录的程序。我在代码中包含了一个后台工作程序,因此我可以异步运行压缩例程,以防止接口冻结。以下是我改编的代码:

private void StartZipping()
{
    var zipWorker = new BackgroundWorker { WorkerReportsProgress = true };

    zipWorker.DoWork += ZipWorkerWork;
    zipWorker.RunWorkerCompleted += ZipWorkerCompleted;

    zipWorker.RunWorkerAsync();
}

private void ZipWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    WriteLogEntry("Zip Worker Finished");
    var sleep = new Timer();
    sleep.Tick += SleepTick;
    sleep.Interval = DEBUG ? SecondsToMilliseconds(45) : MinutesToMilliseconds(15);
    sleep.Start();
}

private void ZipWorkerWork(object sender, DoWorkEventArgs e)
{
    WriteLogEntry("Zip Worker Started");
    var checkedItems = GetCheckedItems();
    if (checkedItems.Count() <= 0) return;

    foreach (var p in checkedItems)
        ZipFiles(((BackupProgram)p.Tag));
}

private static void ZipFiles(BackupProgram program)
{
    var zipPath = string.Format("{0}{1}.ZIP", TempZipDirectory, program.Name.ToUpper());
    WriteLogEntry(string.Format("Zipping files from '{0}' to '{1}'...", program.Path, zipPath));
    try
    {
        var emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        if (File.Exists(zipPath)) File.Delete(zipPath);

        var fs = File.Create(zipPath);
        fs.Write(emptyzip, 0, emptyzip.Length);
        fs.Flush();
        fs.Close();
        //fs = null;

        //Copy a folder and its contents into the newly created zip file
        var sc = new Shell32.ShellClass();
        var srcFlder = sc.NameSpace(program.Path);
        var destFlder = sc.NameSpace(zipPath);
        var items = srcFlder.Items();
        destFlder.CopyHere(items, 20);
        //sc = null;

        System.Threading.Thread.Sleep(1000);
        ZippedPrograms.Add(zipPath);
        WriteLogEntry("Zip Succeeded");
    }
    catch (Exception ex)
    {
        WriteLogEntry(string.Format("Zipping Failed: {0} >> {1}", ex.Message, ex.InnerException.Message));
        MessageBox.Show(ex.InnerException.Message, ex.Message);
    }
}

internal class BackupProgram
{
    public string Name { get; set; }
    public string Path { get; set; }

    public BackupProgram(string name, string path)
    {
        Name = name;
        Path = path;
    }
}
我想知道的是:是否有某种方法可以使用我最初修改的代码,但隐藏进度窗口

谢谢

编辑 以下是示例解决方案的代码(可从文章顶部的链接下载),即zip.exe:

using System;
using System.IO;

namespace zip
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            //Create an empty zip file
            byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            FileStream fs = File.Create(args[1]);
            fs.Write(emptyzip, 0, emptyzip.Length);
            fs.Flush();
            fs.Close();
            fs = null;

            //Copy a folder and its contents into the newly created zip file
            Shell32.ShellClass sc = new Shell32.ShellClass();
            Shell32.Folder SrcFlder = sc.NameSpace(args[0]);
            Shell32.Folder DestFlder = sc.NameSpace(args[1]);
            Shell32.FolderItems items = SrcFlder.Items();
            DestFlder.CopyHere(items, 20);

            //Ziping a file using the Windows Shell API creates another thread where the zipping is executed.
            //This means that it is possible that this console app would end before the zipping thread 
            //starts to execute which would cause the zip to never occur and you will end up with just
            //an empty zip file. So wait a second and give the zipping thread time to get started
            System.Threading.Thread.Sleep(1000);

        }
    }
}
使用系统;
使用System.IO;
名称空间压缩
{
/// 
///1类的概要说明。
/// 
一班
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main(字符串[]参数)
{
//创建一个空的zip文件
字节[]emptyzip=新字节[]{80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FileStream fs=File.Create(args[1]);
fs.Write(emptyzip,0,emptyzip.Length);
fs.Flush();
fs.Close();
fs=null;
//将文件夹及其内容复制到新创建的zip文件中
Shell32.ShellClass sc=新的Shell32.ShellClass();
Shell32.Folder SrcFlder=sc.NameSpace(args[0]);
Shell32.Folder DestFlder=sc.NameSpace(args[1]);
Shell32.FolderItems items=SrcFlder.items();
DestFlder.CopyHere(第20项);
//使用WindowsShellAPI对文件进行Ziping将创建另一个执行压缩的线程。
//这意味着这个控制台应用程序可能会在压缩线程之前结束
//开始执行,这将导致压缩永远不会发生,并且最终只会导致
//一个空的zip文件。请稍等,给压缩线程一点时间开始
系统线程线程睡眠(1000);
}
}
}

如果您想在没有窗口、进度或其他UI显示的情况下进行压缩,并且希望在后台隐藏运行它,那么为什么不使用类似dotnetzip的压缩库,只需派生一个线程来调用执行压缩操作工作流所需的逻辑即可


http://dotnetzip.codeplex.com/wikipage?title=CS-示例

此链接提供了一个很好的示例,说明了实现相同目标的方法:

您必须向下滚动链接页面才能看到答案。
希望这有帮助

你真的需要解释你所说的“部分”成功隐藏它的意思。另外,Zip.exe是您程序的一部分吗?你能编辑代码吗?我将进一步了解这些信息。从现在的情况来看,试着使用以下其中一种方法:i.CreateNoWindow=true;i、 WindowStyle=ProcessWindowStyle.Hidden`正如我在OP中所说,当我使用Process.Start(I)时,窗口被隐藏,但压缩并没有完成。当我使用我的代码时,其中一个测试拉链最终大约为70MB。当我使用Process.Start(I)方法时,同一个zip文件的大小大约为5MB,并不包含所有文件。我将添加zip.exe的代码,它是示例程序的一部分,您可以从我文章开头的链接下载。
using System;
using System.IO;

namespace zip
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            //Create an empty zip file
            byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            FileStream fs = File.Create(args[1]);
            fs.Write(emptyzip, 0, emptyzip.Length);
            fs.Flush();
            fs.Close();
            fs = null;

            //Copy a folder and its contents into the newly created zip file
            Shell32.ShellClass sc = new Shell32.ShellClass();
            Shell32.Folder SrcFlder = sc.NameSpace(args[0]);
            Shell32.Folder DestFlder = sc.NameSpace(args[1]);
            Shell32.FolderItems items = SrcFlder.Items();
            DestFlder.CopyHere(items, 20);

            //Ziping a file using the Windows Shell API creates another thread where the zipping is executed.
            //This means that it is possible that this console app would end before the zipping thread 
            //starts to execute which would cause the zip to never occur and you will end up with just
            //an empty zip file. So wait a second and give the zipping thread time to get started
            System.Threading.Thread.Sleep(1000);

        }
    }
}