Reputation: 503
I get an error when I try to copy a file from one location to another ONLY WHEN the file is already present in the destination. This happens inspite of setting the overwrite flag to true.
I am not getting any clue. This is the code.
System.IO.File.Copy(source, destination, true);// goes fine as the destination file is not present
System.IO.File.Copy(source, destination, true);// Throws the exception
//"Access to the path 'C:\\Program Files (x86)\\testapp\\map\\sometext.txt' is denied."
Please guide.
Thanks Sunil
Upvotes: 1
Views: 4100
Reputation: 416131
This exception is covered in the documentation for File.Copy:
The caller does not have the required permission.
-or-
destFileName is read-only.
Check the attributes of the file after the first copy. Are the permissions what you expect? Do you need your program to run elevated (as administrator)?
Upvotes: 5
Reputation: 13410
If everything is fine, e.g. the file is not already open and path exists etc... this usually means that the user you run the program with is not allowed to do this action. (simply file/folder permissions) What kind of program is this? Is it a web application? If so, you would have to figure out which user is used to run the app pool and give permissions to that user.
Upvotes: 1