문제

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

도움이 되었습니까?

해결책

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)?

다른 팁

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.

Did you check the file is in read only mode or not ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top