title: C# Windows Service Not Compressing Folder Correctly
body: <p>Im currently building a Windows service that will be used to create backups of logs. Currently, the logs are stored at the path <code>E:\Logs</code> and intent is to copy  the contents, timestamp their new folder and compress it. After this, you should have <code>E:\Logs</code> and <code>E:\Logs_[Timestamp].zip</code>. The zip will be moved to C:\Backups\ for later processing. Currently, I am using the following to try and zip the log folder:</p>
  
  <pre><code>var logDirectory = "E://Logs";
  var timeStamp = DateTime.Now.ToString("yyyyMMddHHmm");
  var zippedFolder = logDirectory + "_" + timeStamp + ".zip"; 
  
  System.IO.Compression.ZipFile.CreateFromDirectory(logDirectory, zippedFolder); 
  </code></pre>
  
  <p>While this appears to create a zip folder, I get the error <code>Windows cannot open the folder. The Compressed (zipped) Folder E:\Logs_201805161035.zip is invalid.</code> </p>
  
  <p>To address any troubleshooting issues, the service is running with an AD account that has a sufficient permission level to perform administrative tasks. Another thing to consider is that the service kicks off when its FileSystemWatcher detects a new zip folder in the path <code>C:\Aggregate</code>. Since there are many zip folders that are added to <code>C:\Aggregate</code> at once, the FileSystemWatcher creates a new Task for each zip found. You can see how this works in the following:</p>
  
  <pre><code>    private void FileFoundInDrops(object sender, FileSystemEventArgs e)
      {          
          var aggregatePath = new DirectoryInfo("C://Aggregate"); 
  
          if (e.FullPath.Contains(".zip"))
          {
              Task task = Task.Factory.StartNew(() =&gt;
              {
                  try
                  {
                      var logDirectory = "E://Logs";
                      var timeStamp = DateTime.Now.ToString("yyyyMMddHHmm");
                      var zippedFolder = logDirectory + "_" + timeStamp + ".zip"; 
                      ZipFile.CreateFromDirectory(logDirectory, zippedFolder);                       
                  }
                  catch (Exception ex)
                  {
                      Log.WriteLine(System.DateTime.Now.ToString() + " - ERROR: " + ex);
                  }
              });
  
              task.Dispose();
          }           
      }
  </code></pre>
  
  <p>How can I get around the error I am receiving? Any help would be appreciated!</p>