I realize there are several variations of this question, but in reviewing them I don't see my exact use case and my result has a wrinkle.
What I'm doing is FTPing some BZ2 zipped files to my hard drive in a folder. Then I unzip each one for review. Finally, I want to email the files to someone but I want to put them into a Zip Archive first to make it easier. Below is the code.
private void buttonCreateZip_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\");
string startPath = @"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString();
string zipPath = @"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\" + comboBoxDirectory.SelectedItem.ToString() + ".zip";
File.SetAttributes(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\zip\\", FileAttributes.Normal);
File.SetAttributes(@"C:\temp\logfiles\" + comboBoxDirectory.SelectedItem.ToString() + "\\", FileAttributes.Normal);
ZipFile.CreateFromDirectory(startPath, zipPath);
}
I'm not combining the files individually but using an API that takes all the files in a target directory and zips them into an archive.
The wrinkle is that even though I get the exception
Process cannot access the file "C:..." because it is being used by another process"
it creates the ZIP archive anyway in the zip subdirectory I've created for this purpose. It almost looks like it's inside the library function CreateFromDirectory, but this is part of a standard library which I accessed by referencing:
System.IO.Compression.FileSystem.