title: writing file using File.WriteAllBytes giving error while accessing file created by this function
body: <p>I have used the following code and it works fine. But sometimes it throws an exception:</p>
  
  <pre><code>Message :- The process cannot access the file   'DownloadDB\client\user\filename.db' because it is being used by another process.
  
  Stack Trace :- 
  at System.IO.File.InternalDelete(String path, Boolean checkHost)
  at System.IO.File.Delete(String path)
  at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream,    String password)
  at Ionic.Zip.ZipEntry.Extract(String baseDirectory)
  at Ionic.Zip.ZipFile._InternalExtractAll(String path, Boolean overrideExtractExistingProperty)
  at Ionic.Zip.ZipFile.ExtractAll(String path, ExtractExistingFileAction extractExistingFile)
  at ecpMobileToWebSync.UnZipFiles(String strZipFilePath, String CombinePath, String fk_EmpGLCode)
  </code></pre>
  
  <p>My code is given below:</p>
  
  <pre><code>    private string SyncDB(string FileArray)
      {
          string pathNew = "";
          byte[] encodedDataAsBytesSyncDB = System.Convert.FromBase64String(FileArray);
          pathNew = Server.MapPath("~/SyncDB/" + strClientName_File + "/" + UserName + "/" + Filename);
          File.WriteAllBytes(pathNew, encodedDataAsBytesSyncDB);
  
          byte[] encodedDataAsBytesDownloadDB = System.Convert.FromBase64String(FileArray);
          pathNew = Server.MapPath("~/DownloadDB/" + strClientName_File + "/" + UserName + "/" + Filename);
          File.WriteAllBytes(pathNew, encodedDataAsBytesDownloadDB);
  
          encodedDataAsBytesSyncDB = null;
          encodedDataAsBytesDownloadDB = null;
          FileArray = null;
  
          string strFileName = string.Empty;
          if (File.Exists(pathNew))
          {
              strFileName = UnZipFiles(pathNew, Server.MapPath("~/DownloadDB/" + strClientName_File + "/" + UserName + "/"));
          }
  
      }
      private string UnZipFiles(string strZipFilePath, string CombinePath)
      {
          string strZilEntryFileName = string.Empty;
          ReturnData objReturnData = new ReturnData();
          objReturnData.isValid = true;
          objReturnData.Message = "";
          try
          {
              using (ZipFile zip1 = ZipFile.Read(strZipFilePath))
              {
                  strZilEntryFileName = zip1.Entries.Single().FileName;
                  var flattenFoldersOnExtract = zip1.FlattenFoldersOnExtract;
                  zip1.FlattenFoldersOnExtract = true;
                  zip1.ExtractAll(CombinePath, ExtractExistingFileAction.OverwriteSilently);
                  zip1.FlattenFoldersOnExtract = flattenFoldersOnExtract;
              }
          }
          catch (Exception ex)
          {
              ErrorLogDetails.LogException(this.GetType().Name.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString());
              objReturnData.isValid = false;
              objReturnData.Message = MsgException_Failure;
          }
          return strZilEntryFileName;
      }
  </code></pre>
  
  <p>In this code I am getting a byte array of a zip file from a client and then I am performing reverse engineering for converting the byte array to the zip file and then extracting it. </p>
  
  <p>First I will call the SyncDB function with the byte array and then calling UnZipFiles for unzip the file.</p>
  
  <p>It's all working fine but in some cases it will give an exception.</p>