title: Getting "The process cannot access the file because it is being used by another process" when saving multiple files from the request stream
body: <p>I am using the HTML5 canvas element and the new HTML5 file i\o function to drop multiple files on it and have them upload. It works fine, but now I need to generate a new filename if no files are in the destination directory (It's a 7 digit integer) or get the name of the last uploaded file, convert it to int32 and increment that by one for every new file being uploaded to the same directory. This is where the <code>GetFileName(dir);</code> comes in. The first image always uploads fine but the problem begins once the second file is saved and the process hits ImageJob.Build(), I presume this is because once the new file is starting to write, the GetFile() method runs for second file in line simultaneously and is  checking for last written file, which is still being written and this creates the conflict. How can I fix this, maybe I can somehow itterate with a foreach over the Request.InputStream data or implement some kind process watch that waits for the process to finish?</p>
  
  <p><strong><code>Update:</code></strong> I tried using TempData to store the generated filename, and just increment on the int value in TempData for all the next file names and it appears to do better, gets more images in but still errors at some point. But TempData is not for that as it gets erased after each read, reassigning to it again does not help. Maybe I'll try storing it in session.</p>
  
  <blockquote>
    <p>The process cannot access the file 'C:\Users\Admin\Documents\Visual Studio
    2010\Projects\myproj\myproj\Content\photoAlbums\59\31\9337822.jpg'
    because it is being used by another process.</p>
  </blockquote>
  
  <pre><code>public PartialViewResult Upload()
  {          
      string fileName = Request.Headers["filename"];
      string catid = Request.Headers["catid"];
      string pageid = Request.Headers["pageid"];
      string albumname = Request.Headers["albumname"];
  
  
      var dir = "~/Content/photoAlbums/" + catid + "/" + pageid + "/" + (albumname ?? null);
  
      var noex = GetFileName(dir);
  
      var extension = ".jpg";
  
      string thumbFile = noex + "_t" + extension;
      fileName = noex + extension;
  
      byte[] file = new byte[Request.ContentLength];
      Request.InputStream.Read(file, 0, Request.ContentLength);
  
      string imgdir;
      string thumbimgdir;
      string imageurl;
  
      if (albumname != null) 
      { 
          imgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + fileName);
          thumbimgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + thumbFile);
          imageurl = "/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + thumbFile;
      }
      else 
      { 
          imgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + fileName);
          thumbimgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + thumbFile);
          imageurl = "/Content/photoAlbums/" + catid + "/" + pageid + "/" + thumbFile;
      }
  
      ImageJob b = new ImageJob(file, imgdir, new ResizeSettings("maxwidth=1024&amp;maxheight=768&amp;format=jpg")); b.CreateParentDirectory = true; b.Build();
      ImageJob a = new ImageJob(file, thumbimgdir, new ResizeSettings("w=100&amp;h=100&amp;mode=crop&amp;format=jpg")); a.CreateParentDirectory = true; a.Build();
  
      ViewBag.CatID = catid;
      ViewBag.PageID = pageid;
      ViewBag.FileName = fileName;
  
      return PartialView("AlbumImage", imageurl);
  }
  
  public string GetFileName(string dir)
  {
      var FullPath = Server.MapPath(dir);
      var dinfo = new DirectoryInfo(FullPath);
  
      string FileName;
      if (dinfo.Exists)
      {           
         var Filex = dinfo.EnumerateFiles().OrderBy(x =&gt; x.Name).LastOrDefault();
         FileName = Filex != null ? Path.GetFileNameWithoutExtension(Filex.Name) : null;
  
         if (FileName != null)
         {
             FileName = FileName.Contains("_t") ? FileName.Substring(0, FileName.Length - 2) : FileName;
             int fnum;
             Int32.TryParse(FileName, out fnum);
             FileName = (fnum + 1).ToString();
  
             if (fnum &gt; 999999) { return FileName; } //Check that TryParse produced valid int
             else 
             {
                var random = new Random();
                FileName = random.Next(1000000, 9999000).ToString();
             }
         }
         else
         {
             var random = new Random();
             FileName = random.Next(1000000, 9999000).ToString();
         }
      }
      else
      {        
             var random = new Random();
             FileName = random.Next(1000000, 9999000).ToString();
      }
      return FileName;
  }
  </code></pre>