【发布时间】:2012-11-04 07:52:53
【问题描述】:
我修改了this article 中的代码来制作一个压缩和复制目录的程序。我在我的代码中包含了一个后台工作程序,因此我可以异步运行压缩例程以防止界面冻结。这是我改编的代码:
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;
}
}
显然,就目前而言,所有压缩窗口都会在点击此代码时显示。我试图模仿示例程序(隐藏窗口),但只取得了微不足道的成功。窗户被隐藏了,但拉链只完成了部分。这是修改后的代码,其行为与文章的几乎相同:
private static void ZipFiles(BackupProgram program)
{
try
{
var zipPath = string.Format("{0}{1}.ZIP", TempZipDirectory, program.Name.ToUpper());
var i = new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + "zip.exe");
i.Arguments = string.Format("\"{0}\" \"{1}\"", program.Path, zipPath);
//i.CreateNoWindow = false;
//i.WindowStyle = ProcessWindowStyle.Normal;
i.CreateNoWindow = true;
i.WindowStyle = ProcessWindowStyle.Hidden;
if (File.Exists(zipPath))
{
WriteLogEntry(string.Format("'{0}' exists, deleting...",zipPath));
File.Delete(zipPath);
}
var process = Process.Start(i);
WriteLogEntry(string.Format("Zipping files from '{0}' to '{1}'...", program.Path, zipPath));
process.WaitForExit();
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);
}
}
我想知道的是:有没有办法使用我原来的改编代码,但隐藏进度窗口?
谢谢!
编辑
这是示例解决方案中的代码(可从文章顶部的链接下载),即 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);
}
}
}
【问题讨论】:
-
您真的需要解释一下“部分”成功隐藏它的意思。另外,Zip.exe 是您程序的一部分吗?你可以编辑代码吗?我会根据这些信息进一步调查。从你现在所拥有的,尝试只使用以下一种或另一种:i.CreateNoWindow = true; i.WindowStyle = ProcessWindowStyle.Hidden;`
-
就像我在 OP 中所说的,当我使用 Process.Start(i) 时,窗口会被隐藏,但压缩没有完成。当我使用我的代码时,其中一个测试 zip 最终大小约为 70 MB。当我使用 Process.Start(i) 方法时,相同的 zip 文件最终大小约为 5 MB,并且不包含所有文件。我将添加 zip.exe 的代码,它是示例程序的一部分,您可以从我文章开头的链接下载。
标签: c# backgroundworker