title: IO Exception - File In Use By Another Process
body: <p>C# / .NET 3.5, WindowsForms.</p>
  
  <p>I have this Windows form that displays an image from a file, and whenever user saves the record this code is executed:</p>
  
  <pre><code>                    string oldLoc = itemsBO.ImageLoc;
                      if (oldLoc != SystemSettings.NoImageLocation)
                      {
                          if (File.Exists(oldLoc))
                          {
                              try { File.Delete(oldLoc); }
                              catch (IOException ex)
                              {
                                  MessageBox.Show("1 - "  + ex.GetType().ToString() + "    " + ex.Message);
                              }
                          }
                      }
  
                      string saveLoc = itemsBO.ImageSearchLoc + ".jpg";
                      if (File.Exists(saveLoc))
                      {
                          try { File.Delete(saveLoc); }
                          catch (IOException ex)
                          {
                              MessageBox.Show("2 - " + ex.GetType().ToString() + "    " + ex.Message);
                          }
                      }
  
                      try
                      {
                          if (pictureBox2.Image != null)
                              pictureBox2.Image.Save(saveLoc, System.Drawing.Imaging.ImageFormat.Jpeg);
                      }
                      catch (IOException ex)
                      {
                          MessageBox.Show("3 - " + ex.GetType().ToString() + "    " + ex.Message);
                      }
  </code></pre>
  
  <p>Disregard the poor MessageBox messages, but it errors out in each Catch statement. It can't delete the "existing" Image because it says it's in use by another process. Can't save because a file exists in that same path because it's not deleting.</p>
  
  <p>This is the code that sets the Image when they try to add a new picture;</p>
  
  <pre><code>            Image clipImage = Clipboard.GetImage();
  
              if (tabControl2.SelectedTab == tabPage5)
              {
                  pictureBox1.Image = clipImage;
                  itemsBO.IsDirtyImage = true;
              }
              else if (tabControl2.SelectedTab == tabPage6)
              {
                  pictureBox2.Image = clipImage;
                  itemsBO.IsDirtyImage2 = true;
              }
  </code></pre>
  
  <p>Then when the form loads up an existing record with an image, this is the code used to fetch/display it:</p>
  
  <pre><code>            byte[] bits = File.ReadAllBytes(imgfil);
              msImage = new MemoryStream(bits, 0, bits.Length);
  
              if (tabControl2.SelectedTab == tabPage5)
                  pictureBox1.Image = Image.FromStream(msImage);
              else if (tabControl2.SelectedTab == tabPage6)
                  pictureBox2.Image = Image.FromStream(msImage);
  </code></pre>
  
  <p>imgfil being a path to the image, of course.</p>
  
  <p>Absolutely no idea where to begin...</p>