title: System.IO.File.Copy Error with file not found
body: <p>I am trying to create a image upload that can copy the image into a folder. The problem is that when I try to copy the photo to a folder, there is an error which says that it could not find the file. My code is below:</p>
  
  <pre><code>private void btn_Click(object sender, EventArgs e)
      {
          DialogResult result = folderBrowserDialog1.ShowDialog();
          if (result == DialogResult.OK)
          {
              //
              // The user selected a folder and pressed the OK button.
              // We print the number of files found.
              //
              string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
  
          }
  
          if (result == DialogResult.Cancel)
          {
              return;
          }
  
              //001: Check the user selected a file and pressed ok button. Before opening the dialog
              //      set the filters
              dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
  
              string path = folderBrowserDialog1.SelectedPath;
              if (dlgFileOpen.ShowDialog() == DialogResult.OK)
              {
                 // string file = Path.GetFileName(dlgFileOpen.FileName);
                  string file = Path.GetFileName(dlgFileOpen.FileName);
                  //string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
                  string newFileName = System.IO.Path.Combine(path, file);
                  System.IO.File.Copy(file, newFileName, true);
  
  
                  //image directory to be saved in database
                  //txtSelectedFile.Text = newFileName;
              }
          }
  
  
      private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
      {
          string Required_Ext = ".jpeg .jpg .bmp";
          string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
          int index = Required_Ext.IndexOf(selected_ext);
          //002: Inform the user to select correct extension
          if (index &lt; 0)
          {
              MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
              e.Cancel = true;
          }
      }
  </code></pre>
  
  <p>ANSWER:</p>
  
  <pre><code>private void btn_Click(object sender, EventArgs e)
  
      {
  
          DialogResult result = folderBrowserDialog1.ShowDialog();
          if (result == DialogResult.OK)
          {
              //
              // The user selected a folder and pressed the OK button.
              // We print the number of files found.
              //
              string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
  
          }
  
          if (result == DialogResult.Cancel)
          {
              return;
          }
  
              //001: Check the user selected a file and pressed ok button. Before opening the dialog
              //      set the filters
              dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
  
              string path = folderBrowserDialog1.SelectedPath;
              if (dlgFileOpen.ShowDialog() == DialogResult.OK)
              {
                 // string file = Path.GetFileName(dlgFileOpen.FileName);
                  string file = Path.GetFileName(dlgFileOpen.FileName);
                  //string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
                  string newFileName = System.IO.Path.Combine(path, file);
                  System.IO.File.Copy(dlgFileOpen.FileName, newFileName, true);
  
                  txtSelectedFile.Text = newFileName;
                  MessageBox.Show("Image uploaded successful!");
              }
          }
  
  
      private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
      {
          string Required_Ext = ".jpeg .jpg .bmp";
          string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
          int index = Required_Ext.IndexOf(selected_ext, StringComparison.CurrentCultureIgnoreCase);
          //002: Inform the user to select correct extension
          if (index &lt; 0)
          {
              MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
              e.Cancel = true;
          }
      }
  </code></pre>