title: Process cannot access the file because it is being used by another process on second call
body: <p>I have an Azure Function (HttpTrigger) which creates a tar.gz file in my Azure Storage.</p>
  
  <p>When I trigger it one time, the file is succesfully created. but if I hit it again I have an error:</p>
  
  <blockquote>
    <p>[Info] The process cannot access the file
    'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by
    another process.</p>
  </blockquote>
  
  <p>I have to restart the function in the Azure Portal to be able to create another file.</p>
  
  <p>here is my code:</p>
  
  <pre><code>FileStream fs = new FileStream(myDir+"/firstFile", FileMode.Create);
  fs.Write(bytesToCompress, 0, bytesToCompress.Length);
  fs.Dispose();
  FileStream sfs = new FileStream(myDir + "/secondfile", FileMode.Create);
  sfs.Dispose();
  
  DirectoryInfo DirectoryOfFilesToBeTarred = new DirectoryInfo(myDir);
  FileInfo[] filesInDirectory = DirectoryOfFilesToBeTarred.GetFiles();
  
  string tarArchiveName = myDir + "/tarArchive.tar.gz";
  
  using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
   {
      using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
      {
          foreach(FileInfo fileToBeTarred in filesInDirectory)
          {
             log.Info(fileToBeTarred.FullName);
             TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);                            
             tarArchive.WriteEntry(entry, true); // Error thrown here
  
          }
      }
   }
  </code></pre>
  
  <p>I thought fileToBeTarred was still in use when the function is called again (am I wrong?) but I've tried to create a stream from this FileInfo in order to Dispose() it, but didn't fix my problem. I've also tried to Delete() it, without any effect.</p>
  
  <p>Is someone seeing what I don't see ?</p>
  
  <p>Thanks for your help </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is the corected code given by Wim Coenen</p>
  
  <pre><code>using (Stream fileStream = File.Create(tarArchiveName))
  using (Stream targetStream = new GZipOutputStream(fileStream))
  using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
   {
     foreach (FileInfo fileToBeTarred in filesInDirectory)
     {
       log.Info(fileToBeTarred.FullName);
       TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
       tarArchive.WriteEntry(entry, true); // Error thrown here 
     }
  }
  </code></pre>
  
  <p>And the log of the error (e = Exception object)
  e.message =></p>
  
  <blockquote>
    <p>2018-08-01T11:59:46.887 [Info] The process cannot access the file
    'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by
    another process.</p>
  </blockquote>
  
  <p>e.ToString() => </p>
  
  <blockquote>
    <p>2018-08-01T11:59:47.152 [Info] System.IO.IOException: The process
    cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz'
    because it is being used by another process.</p>
    
    <p>at System.IO.__Error.WinIOError(Int32 errorCode, String
    maybeFullPath)</p>
    
    <p>at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
    access, Int32 rights, Boolean useRights, FileShare share, Int32
    bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String
    msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)</p>
    
    <p>at System.IO.FileStream..ctor(String path, FileMode mode,
    FileAccess access, FileShare share)</p>
    
    <p>at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntryCore(TarEntry
    sourceEntry, Boolean recurse)</p>
    
    <p>at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntry(TarEntry
    sourceEntry, Boolean recurse)</p>
    
    <p>at UploadFileFromBCText.Function1.d__4.MoveNext()</p>
  </blockquote>