我的问题是:
我需要编写一个压缩一个文件的应用程序。
应用程序成功地从用户定义的位置读取文件,但问题是,我无法将存档文件名写入指定文件夹而不会出现烦人的错误:"The process cannot access the file 'C:\Users\bg\Desktop\1323.zip
'因为它正在被另一个进程使用。”在这一行:archiver.OpenArchive( System.IO.FileMode.Create
);
这是我的c尖锐代码:
private void zipThatFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
ZipForge archiver = new ZipForge();
// The name of the ZIP file to be create
string env = saveFileDialog1.FileName;
//MessageBox.Show(env);
//this doesn't work
archiver.FileName = env;
//this works
//archiver.FileName = @"D:\test.zip";
// Specify FileMode.Create to create a new ZIP file
// or FileMode.Open to open an existing archive
archiver.OpenArchive (System.IO.FileMode.Create);
// Default path for all operations
archiver.BaseDir = @"C:\Users\bg\Desktop\";
// Add file C:\file.txt the archive; wildcards can be used as well
archiver.AddFiles(openFileDialog1.FileName);
// Close archive
archiver.CloseArchive();
MessageBox.Show("The archive was created! ");
myStream.Close();
}
}
我正在使用这个http://www.componentace.com/zip-file-in-c-sharp.htm和这个http://www.componentace.com/zip-file-in-c-sharp.htm