My problem is :
I need to write an application that zips one file.
The application successfully reads the file from an user defined location, but the trouble is, I can’t make it to write the archive file name to a specified folder without getting an annoying error : "The process cannot access the file 'C:\Users\bg\Desktop\1323.zip‘ because it is being used by another process.” at the line :
archiver.OpenArchive (System.IO.FileMode.Create);
Here is my c sharp code :
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();
}
}
I’m using this http://www.componentace.com/zip-file-in-c-sharp.htm with this http://www.componentace.com/zip-file-in-c-sharp.htm
I can’t say for sure, but it looks as if you’re trying to open the same file twice — first when you call
"myStream = saveFileDialog1.OpenFile()) != null", and again when you call
"archiver.OpenArchive (System.IO.FileMode.Create);"I’d suggest perhaps closing myStream before you start your archive operations.