title: How to zip a folders contents in C# even if a file is in use
body: <p>I am learning C# and want to create a simple backup program using C# winforms.</p>
  
  <p>I have got the program to zip the folder and move it to another location no problems when the files arent in use. How can I do the same if the files are in use or at least ignore the file in use and keep zipping the rest.</p>
  
  <p>My ideal solution would be to copy and zip all files even if they are in use, failing that I want to zip everything it can and print a list of the files that it missed.</p>
  
  <p>Here is the code I am using to zip and send to another location</p>
  
  <pre><code> private void btnZip_Click(object sender, EventArgs e)
      {
          string fromDirectory = txtBackupFrom.Text;
          string todaysDate = DateTime.Now.ToString("dd_MMM_yyyy");
          string toDirectory = txtBackupToLocation.Text + "/" + todaysDate + "_backup.zip";
  
          try
          {
              ZipFile.CreateFromDirectory(fromDirectory, toDirectory, CompressionLevel.Optimal, true);
              //Write the current date to the text file
  
              StreamWriter writer = new StreamWriter(runningDirectory + "\\BackupDate.txt"); //open the file for writing.
              writer.Write(DateTime.Now.ToString("dd MMM yyyy")); //write the current date to the file. change this with your date or something.
              writer.Close(); //remember to close the file again.
              writer.Dispose(); //remember to dispose it from the memory.
  
              MessageBox.Show("Backup Successful");
          }
          catch (Exception zipException)
          {
              MessageBox.Show(zipException.Message);
          }
      }
  </code></pre>
  
  <p>/********EDITED BELOW*************/</p>
  
  <p>Here is what I have tried to fix the issues. It may not be the best way as I am learning but it is working</p>
  
  <pre><code>private void btnZip_Click(object sender, EventArgs e)
      {
          //copy location before zipping
          string copytoBeforZippingDirectory = txtBackupToLocation.Text;
          //temporary folder for zipping
          string copytotempDirectory = Path.Combine(copytoBeforZippingDirectory, "Backup_File");
          //where the files are coming from
          string copyfromDirectory = txtBackupFrom.Text;
          //today's date
          string todaysDate = DateTime.Now.ToString("dd_MMM_yyyy");
          //Where the files will end up
          string copytoDirectory = txtBackupToLocation.Text + "\\" + todaysDate + "_backup.zip";
  
          //Create the new temporary folder for zipping
          Directory.CreateDirectory(copytotempDirectory);
  
          try
          {
              //Get a list of all files
              string[] fileList = Directory.GetFiles(copyfromDirectory, "*");
              // Copy all files. 
              foreach (string f in fileList)
              {
                  // Remove path from the file name. 
                  string fName = f.Substring(copyfromDirectory.Length + 1);
                  // Use the Path.Combine method to safely append the file name to the path. 
                  // Will overwrite if the destination file already exists.
                  File.Copy(Path.Combine(copyfromDirectory, fName), Path.Combine(copytotempDirectory, fName), true);
              }
              MessageBox.Show("Files Copied successfully. Beginning Zipping of Files");
          }
  
          catch (DirectoryNotFoundException dirNotFound)
          {
              MessageBox.Show(dirNotFound.Message);
              Close();
          }
          //Zipping the temporary folder
          try
          {
              ZipFile.CreateFromDirectory(copytotempDirectory, copytoDirectory, CompressionLevel.Optimal, true);
              //Write the current date to the text file
              StreamWriter writer = new StreamWriter(runningDirectory + "\\BackupDate.txt"); //open the file for writing.
              writer.Write(DateTime.Now.ToString("dd MMM yyyy")); //write the current date to the file. change this with your date or something.
              writer.Close(); //remember to close the file again.
              writer.Dispose(); //remember to dispose it from the memory.
  
              MessageBox.Show("Backup Successful");
          }
          catch (Exception zipException)
          {
              MessageBox.Show(zipException.Message);
          }
  
          //Remove the temp folder
          try
          {
              Directory.Delete(copytotempDirectory, true);
          }
          catch (Exception)
          {
              MessageBox.Show("Temporary Folder " + copytotempDirectory + " not removed");
          }
      }
  </code></pre>