title: File.Move throws error when used with BackgroundWorkerr in C#
body: <h2>Solved</h2>
  <blockquote>
  <p>I figured out that the GetNewFolderNameBasedOnDate method internally didn't close the file. I have that method fixed and it working normal now</p>
  </blockquote>
  <p>I am trying to move selected files from one folder to another using BackgroundWorker process in C#. Here is my DoWork() method that determine whether to move the files or just copy. My File.Move() throws an exception that &quot;The process cannot access the file because it is being used by another process&quot;. I tried different methods as mentioned in the threads here in stackoverflow.</p>
  <pre><code>private void FileProcessor_DoWork(object sender, DoWorkEventArgs e)
  {
      // Copy files
      long bytes = 0;
      string destSubFolder = String.Empty;
      string destFile = string.Empty;
      foreach (FileInfo file in oSettings.SourceFiles)
      {
          try
          {
              this.BeginInvoke(OnChange, new object[] { new UIProgress(file.Name, bytes, oSettings.MaxBytes) });
  
              destSubFolder = GetNewFolderNameBasedOnDate(file);
                      
              //Create a new subfolder under the current active folder
              string newPath = Path.Combine(oSettings.TargetFolder, destSubFolder);
              // Create a new target folder, if necessary.
              if (!System.IO.Directory.Exists(newPath))
              {
                  System.IO.Directory.CreateDirectory(newPath);
              }
              destFile = Path.Combine(oSettings.TargetFolder, destSubFolder, file.Name);
  
              if (chkDeleteSourceFiles.Checked)
              {
                  FileInfo f = new FileInfo(file.FullName);
  
                  if (f.Exists)
                  {
                      File.Move(file.FullName, destFile);
                  }
              }
              else
              {
                  File.Copy(file.FullName, destFile, true);
              }
  
              //Thread.Sleep(2000);
          }
          catch (Exception ex)
          {
              UIError err = new UIError(ex, file.FullName);
              this.Invoke(OnError, new object[] { err });
              if (err.result == DialogResult.Cancel) break;
          }
          bytes += file.Length;
      }
  }
  </code></pre>
  <p>I tried to delete the files in &quot;RunWorkerCompleted&quot; method too. But didn't resolve the problem. This fails when it tries to delete the last file in the list.</p>
  <pre><code>private void FileProcessor_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
      // Operation completed, update UI
      ChangeUI(false);
  
      foreach (FileInfo file in oSettings.SourceFiles)
      {
          File.Delete(file.FullName);
      }
  }
  </code></pre>
  <p>GetNewFolderNameBasedOnDate() calls GetDateTaken() which was the culprit. Earlier I didn't use FileStream object but used <code>Image myImage = Image.FromFile(filename);</code> I didn't know that Image.FromFile locks the file.</p>
  <pre><code>private DateTime GetDateTaken(string fileName)
  {
      try
      {
          using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
          {
              Image myImage = Image.FromStream(fs);
              PropertyItem propItem = myImage.GetPropertyItem(36867);
              DateTime dtaken;
  
              //Convert date taken metadata to a DateTime object
              string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
              string secondhalf = sdate.Substring(sdate.IndexOf(&quot; &quot;), (sdate.Length - sdate.IndexOf(&quot; &quot;)));
              string firsthalf = sdate.Substring(0, 10);
              firsthalf = firsthalf.Replace(&quot;:&quot;, &quot;-&quot;);
              sdate = firsthalf + secondhalf;
              dtaken = DateTime.Parse(sdate);
              return dtaken;
          }
      }
      catch (Exception ex)
      {
          return DateTime.Now;
      }
  }
  </code></pre>