title: File cannot be accessed because it is used by another process exception
body: <p>I am doing image Read/Copy operations in WPF application. Please look at following piece of code:</p>
  
  <pre><code>try
  {
    if (sourceDir != "")
      File.Copy(sourceDir, Path.Combine(backupDir, ecode + ".jpg"), true);
  }
  catch (Exception exx)
  {
      MessageBox.Show(exx.ToString());
  }
  </code></pre>
  
  <p>Now let explain problem with a scenario: <br/>First time when this code executes the <code>sourceDir</code> and <code>Path.Combine(backupDir, ecode + ".jpg")</code> values are:<br/>
  <b>sourceDir</b>="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"<br/>
  <b>Path.Combine(backupDir, ecode + ".jpg")</b>="D:\IEPL-archives-Do not Modify\DATA\654.jpg"
  <br/>
  It works fine for the first time and file is being copied to its destination folder.
  <br/><br/>
  But for the second time when this code executes with following values:<br/>
  <b>sourceDir</b>="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"<br/>
  <b>Path.Combine(backupDir, ecode + ".jpg")</b>="D:\IEPL-archives-Do not Modify\DATA\654.jpg"
  <br/>
  It throws following exception:</p>
  
  <p><img src="https://i.stack.imgur.com/coFhE.png" alt="enter image description here"></p>
  
  <p>I am also displaying same image in UI, that is causing this exception. Here is the code which displays the image in UI:</p>
  
  <pre><code>image1.Source = new BitmapImage(new Uri(GetPicture(txtBarcode.Text), UriKind.RelativeOrAbsolute));
  private string GetPicture(string _eid)
      {
          string picname = "";
          if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".jpg"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".jpg";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".jpeg"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".jpeg";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".png"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".png";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".gif"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".gif";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".JPG"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".JPG";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".JPEG"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".JPEG";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".PNG"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".PNG";
          else if (File.Exists(@"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".GIF"))
              picname = @"D:\IEPL-archives-Do not Modify\DATA\" + _eid + ".GIF";
          else
              picname = @"Images\defaultPicture.jpg";
          return picname;
      }
  </code></pre>
  
  <p>Please suggest how can i modify the code so that this conflict will not arise?
  Thanks</p>