views:

134

answers:

3

In c#, on a windows 7 machine, how can I programmatically access every file on the file system as though I was a "Master Administrator".

The main priority here, is that after my c# program is installed, that it won't run into any file/folder access permission problems. My program runs as a windows service, and it must allow a user to backup any files on the file system.

+5  A: 

I would have thought that it would work if you just make sure that the service runs under an account in the Backup Operator group. I thought that group had access to all files no matter what permissions there are.

Quote from MS Support page:

SID: S-1-5-32-551 Name: Backup Operators Description: A built-in group. By default, the group has no members. Backup Operators can back up and restore all files on a computer, regardless of the permissions that protect those files. Backup Operators also can log on to the computer and shut it down.

Though that page is for earlier versions of Windows so I'm not 100% certain that it's not been changed.

ho1
I don't think just running with Backup Operator privileges allows you to open any file. I'm pretty sure you need to open the files with FILE_FLAG_BACKUP_SEMANTICS set (and maybe some other flags, too) using the CreateFile API.
Chris Haas
ho1
+2  A: 

There is the Win32 backup API which is most likely what you want, maybe in combination with the Volume Shadow Service. And your application has to have the SeBackupPrivilege privilege. Note, though, that files encrypted with EFS can be read, but only in their encrypted form.

Links which may also be interesting for you:

http://mutable.net/blog/archive/2006/11/21/an-intelligent-backup-system-for-windows-part-3.aspx

http://msdn.microsoft.com/en-us/library/aa362520(v=VS.85).aspx

Lucero
+1  A: 

You will have to configure the service to run under an account with sufficient privileges.

AFAIK the standard 'Local System' already has rather high privileges. But no matter what, you won't be able to access files that are reserved to the System account, or files that are in use exclusively. Your program will always have to be able to handle Access related exceptions.

Henk Holterman