title: C#: Why am I getting "the process cannot access the file * because it is being used by another process" with this code?
body: <p>I'm trying to convert bmp files in a folder to jpg, then delete the old files. The code works fine, except it can't delete the bmp's.</p>
  
  <pre><code>DirectoryInfo di = new DirectoryInfo(args[0]);
  FileInfo[] files = di.GetFiles("*.bmp");
  foreach (FileInfo file in files)
  {
      string newFile = file.FullName.Replace("bmp", "jpg");
      Bitmap bm = (Bitmap)Image.FromFile(file.FullName);
      bm.Save(newFile, ImageFormat.Jpeg);
  }
  for (int i = 0; i &lt; files.Length; i++)
      files[i].Delete();
  </code></pre>
  
  <p>The files aren't being used by another program/process like the error indicates, so I'm assuming the problem is here. But to me the code seems fine, since I'm doing everything sequentially. This is all that there is to the program too, so the error can't be caused by code elsewhere.</p>