title: How to avoid exception The process cannot access the file because it is being used by another process. in .net
body: <p>I am unzipping a.gz files by using the following code.Since i have huge number of files , I am using TPL tasks to run this code. But I used to get <code>.NET</code> exception:</p>
  
  <blockquote>
    <p>The process cannot access the file  because it is being used by another process. </p>
  </blockquote>
  
  <p>How to correct this issue?<br>
  Code used is as follows:</p>
  
  <pre><code>private static void Decompress(FileInfo fileToDecompress)
  {
      using (FileStream originalFileStream = fileToDecompress.OpenRead())
      {
          string currentFileName = fileToDecompress.FullName;
          string newFileName = currentFileName.Remove(currentFileName.Length - 
                                            fileToDecompress.Extension.Length);
          using (FileStream decompressedFileStream = File.Create(newFileName))
          {
              using (GZipStream decompressionStream = 
                       new GZipStream(originalFileStream, 
                                      CompressionMode.Decompress))
              {
                  decompressionStream.CopyTo(decompressedFileStream);
              }
          }
      }
      //File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));
  }
  </code></pre>
  
  <p>calling code is as shown below</p>
  
  <pre><code>var list = ftp.GetFileList(remotepath);
  
  //-------------------
  DateTime dt = DateTime.Now;
  string st = String.Format("{0:yyyyMMdd}", dt);//20161120
  Task[] myTasks = new Task[list.Count];
  int i = 0;
  foreach (string item in list)
  {
       if (item.StartsWith("GExport_") &amp;&amp; (!item.ToUpper().Contains("DUM")) &amp;&amp; (item.Contains(st)) &amp;&amp; (!item.ToUpper().Contains("BLK")))
       {
           4gpath = item;
          //Downloadfile()   
          ftp.Get(dtr["REMOTE_FILE_PATH"].ToString() + 4gpath , @localDestnDir + "\\" + dtr["SOURCE_PATH"].ToString());
          download_location_hw = dtr["LOCAL_FILE_PATH"].ToString();
          // Spin off a background task to process the file we just downloaded
          myTasks[i++] = Task.Factory.StartNew(() =&gt;
          {
              //Extractfile()             
              ExtractZipfiles(download_location_hw + "//" + huwawei4gpath, dtr["REMOTE_FILE_PATH"].ToString(), 
               dtr["FTP_SERVER"].ToString(), dtr["FTP_USER_ID"].ToString(),
               dtr["TECH_CODE"].ToString(), dtr["VENDOR_CODE"].ToString());
          }
      }
  }
  
  private static void ExtractZipfiles(string download_location_hw,string remotepath,string server,string userid,string tech,string vendor)
  {
      if (download_location_hw.Contains(".gz"))
      {
          DirectoryInfo directorySelected = new DirectoryInfo(Path.GetDirectoryName(download_location_hw));
          foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
          {
              Decompress(fileToDecompress);
          }
      }
  
  }
  </code></pre>