0

I am trying to copy one image from one location to another location using File.Copy() function but it gives the process can not access exception,any one can please help on this bellow is the code block.I have attached screenshot for exception.

private void btnUpload_Click(object sender, EventArgs e)
{  
    string SourcePath;
    string RootDrive;
    string DestPath;
    string fileName;
    fileName = "";
        try
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select Image to Upload";
            ofd.Filter = "Jpg|*.jpg|Jpge|*.jpge|Gif|*.gif";
            ofd.FileName = null;
            if (ofd.ShowDialog() != DialogResult.Cancel)
            {
                fileName = ofd.FileName;

            }
            ofd.Dispose();
            DestPath = Directory.GetCurrentDirectory() + @"\Uploads\PropertyImages\";
                string destFile = System.IO.Path.Combine(DestPath, fileName);

                if (!System.IO.Directory.Exists(DestPath))
                {
                    System.IO.Directory.CreateDirectory(DestPath);
                }
                System.IO.File.Copy(fileName, destFile, true);                                   

        }
        catch (Exception ae)
        {
            MessageBox.Show(ae.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

enter image description here

Shashikant
  • 137
  • 1
  • 2
  • 10
  • Some other program is using the file. It's as simple as that. Do you have the file open in another copy of the program? Some other program? – siride Jul 31 '16 at 18:34
  • Also, if you are uploading the file to the same place as it currently is, you'll get that error. Make sure your source path and destination path are different. – siride Jul 31 '16 at 18:37
  • @siride No the file is not used by another program and yes both the paths are different – Shashikant Jul 31 '16 at 18:38
  • Well, it is, because you are getting that error. – siride Jul 31 '16 at 18:39
  • Possible duplicate of [IOException: The process cannot access the file 'file path' because it is being used by another process](http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – Reyan Chougle Jul 31 '16 at 18:43
  • You could find out in the command line cmd: https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows?fbclid=IwAR0eVQnKh1njjyfi7szcr3Ji0oj1ZX6hz5iEZ3RQcs5fvfHuocrGvTkaME8 –  Jan 05 '21 at 02:17

1 Answers1

1

It's probably because you are attempting to copy the file to itself. The call to Combine(), as you have it, is just returning fileName. Change the following line:

string destFile = System.IO.Path.Combine(DestPath, fileName);

to

string destFile = System.IO.Path.Combine(DestPath, System.IO.Path.GetFileName(fileName));
seairth
  • 1,966
  • 15
  • 22
  • @Shashikant - if this answer solved your problems, please be sure to mark as Accepted (click the green checkmark) so that others know this was the correct solution if they come visit later. :) – Tommy Jul 31 '16 at 19:37
  • @seairth done, can you please explain like what that function GetFileName() actually do. – Shashikant Jul 31 '16 at 19:41
  • @Shashikant It returns only the "main_background.jpg" portion of the path stored in `fileName`. – seairth Jul 31 '16 at 19:44
  • So the combine function will not work if we pass the pull path of the file,it will work only when we pass the only file name of the source correct ? – Shashikant Jul 31 '16 at 20:01
  • See MSDN on [Path.Combine(path1, path2)](https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx): _If path2 includes a root, path2 is returned_. – Patrice Gahide Jul 31 '16 at 20:19