0

I'm trying to do an auto-backup program that takes a folder and copies it into another folder. Pretty easy task :

File.Copy(Source, Destination);

Source is something like C:\Users\SomeUser\MyDocuments\SavedFiles
Destination is something like D:\BackUp

But when I run my program , I get "Error with access to the path C:\blabla..... access is denied.

Is there any type of permission I must change to use the Copy method ? I guess so.. I looked a little but I can't find where to change such a thing.
Any Idea ?


UPDATE

enter image description here

phadaphunk
  • 12,785
  • 15
  • 73
  • 107

2 Answers2

0

You have to assume that you might not have permission to access some files and folders on your hard drive.

One solution is to manually run your application with administrator-level permissions, but it is easier to modify the manifest file so that the application automatically seeks admin level when it is run.

You can do this by adding a mainifest file to your project (Add new item...).

Then change the line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />" 

to

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />" 

Obviously, this might not solve your problem if the user who is meant to run the program does not have an administrator-level account.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • I does not work :( Both solution. I tried running as admin before posting here and didn't work either. Also I didn't get the pop up that usually asks for me to accept. On both solutions above.. – phadaphunk Feb 07 '13 at 20:26
  • That's odd. I read your question comment above and it does state in the MSDN documentation for `File.Copy` that the source argument has to be a file so that could definitely be your problem. In case you haven't found a solution for that, try [the answer to this question](http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp). – dandan78 Feb 07 '13 at 22:03
  • Why do you assume that a `File.Copy` operation would (automagically) display a UAC dialog for elevation? – IInspectable Jun 17 '17 at 10:33
0

google Shadow Copy service. Thats how the off the shelf packages work.

user1438082
  • 2,740
  • 10
  • 48
  • 82