Take ownership of a file (programatically)

K

KennethLundin

Hi,

i'm trying to take ownership of a file using C# and .NET 3.5... i'm
trying doing it like this:

FileSecurity fs = new FileSecurity( "filename", AccessControl.Owner);
fs.SetOwner( new System.Security.Principal.NTAccount
( Environent.UserDomainName, Environment.Username));
File.SetAccessControl( "filename", fs);

But it throws already on new FileSecurity: Attempted to perform an
unauthorized operation.

And this might not be wrong, since when I look at the file in explorer
and try to view it's permissions and owner, I can't do that
(permissions listbox are blank and owner field says "Unable to display
current owner"). BUT in explorer, I can take ownership of the file and
then see it's permissions etc.

So I've also tried in my C# program to do something like:
FileSecurity fs = new FileSecurity();
fs.SetOwner( new System.Security.Principal.NTAccount
( Environent.UserDomainName, Environment.Username));
File.SetAccessControl( fs);

Instead of trying to read the existing FileSecurity, just create a new
one and mash it onto the file. But that does not work either, then
SetAccessControl throws with same exception as above...

Conlusion: I do have the rights to take ownership of the file since I
can do it in explorer, but how do I do that programatically in C# ???

//Kenneth
 
Joined
Jan 28, 2009
Messages
1
Reaction score
0
Kenneth,

I have been having exactly the same problem as you. My application is a windows service that processed scanned files that are have had an acess control list set by the scanning software.

The service was running using an administrators account that had take ownership permissions but I was getting the same error as you described. The only way I could get this to work was to run the service as the local system account.

All I can offer. Hope it helps.

Regards,

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top