Click here to Skip to main content
15,889,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am writing this code in windows form where user provides SOURCE path and DESTINATION path manually in textbox.
And there is a button called COPY.

After clicking on copy, the files inside Source folder should copy to Destination folder

But it is throwing an error at source path stating
'Access to the path 'C:\Users\XXXX\Desktop\SOURCE' is denied.'


Can anyone help me on this and below is the code on BUTTON click.

NOTE: On a button click, another form opens. So the code below provided is on form opening on button click

CODE:

private void button2_Click(object sender, EventArgs e)
       {
           source = textBox1.Text;
           destination = textBox2.Text;
           thresholdvalue = textBox3.Text;
           var extensions = new[] { ".tif", ".tiff", ".jpg", ".jpeg" };

           foreach (var sourceFilePath in Directory.GetFiles(source))
           {
               string fileName = Path.GetFileName(sourceFilePath);
               string destinationFilePath = Path.Combine(destination, fileName);

               System.IO.File.Copy(source, destinationFilePath, true);
           }
       }


CODE ERROR IN MAIN PAGE:

private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ReadOPT.Files_Copy_to_DeliveryPath f = new ReadOPT.Files_Copy_to_DeliveryPath();
                f.ShowDialog();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
}


What I have tried:

I tried changing the permissions or owner on the SOURCE folder by right click and properties butsame issue
Posted
Updated 26-Aug-20 19:54pm
Comments
[no name] 26-Aug-20 20:14pm    
"Users" don't typically specify "C:\Users\xxxx" files. Try something else.

1 solution

Seems security based issue. You would need to elevate access permission. Run the application in admin mode. right click and choose 'Run As Admin'

Also, DEBUG to make sure that the file paths for copy are of files and folder itself.

One more thing, just check that the files in folder are not in readonly mode (have heard few complaining this too as one of the possible reasons/fix)

Try out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900