0
我創建選項來備份DotNetZip應用程序的數據,並避免凍結應用程序我發現,這種類型的行動最好的方式的最佳途徑是使用BackgroundWorker。於是,我帶着這樣的:DotNetZip在BackgroundWorker更新表單上的進度條和標籤
private void processButton_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
BackupParams bp = new BackupParams();
bp.Source = inputTextBox.Text; // source dir
bp.Output = outputTextBox.Text; // output file
bp.Password = @"Pa$$w0rd";
worker.RunWorkerAsync(bp);
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show((string)e.Result, "Zip", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackupParams bp = (BackupParams)e.Argument;
string id = Guid.NewGuid().ToString();
comment += "Created at: " + DateTime.Now.ToString() + "\n";
comment += id;
ZipFile zf = new ZipFile();
zf.Comment = comment;
zf.CompressionMethod = CompressionMethod.BZip2;
zf.CompressionLevel = CompressionLevel.BestCompression;
zf.Encryption = EncryptionAlgorithm.WinZipAes256;
zf.Password = bp.Password;
zf.Name = bp.Output;
zf.AddDirectory(bp.Source);
zf.Save();
e.Result = bp.Output;
}
,這是BackupParams
public class BackupParams
{
public string Source { get; set; }
public string Output { get; set; }
public string Password { get; set; }
}
而現在,我堅持,因爲我想顯示添加到文件的進度(百分比名)存檔。做這個的最好方式是什麼?我知道我可以使用這些方法從的ZipFile
zf.SaveProgress += zf_SaveProgress;
zf.AddProgress += zf_AddProgress;
但那些我沒有說是對形式的訪問進度或標籤...
我不得不修改了一下那個lambda表達式來檢查'eventArgs.EventType '如果它是一個'ZipProgressEventType.Adding_afterAddEntry'和'ZipProgressEventType.Saving_AfterWriteEntry',或者它拋出'NullReferenceException' – Dawid
@ gnur2171 done。現在這個代碼適用於我。我有保存進度和保存的文件名稱顯示。正是我在找什麼。 – Dawid