Nie ma potrzeby, aby P/Invoke. System.IO.File.GetAccessControl zwróci obiekt FileSecurity, który ma metodę GetOwner.
Edit: Czytanie właściciela jest dość prosta, choć jest to trochę kłopotliwe API:
const string FILE = @"C:\test.txt";
var fs = File.GetAccessControl(FILE);
var sid = fs.GetOwner(typeof(SecurityIdentifier));
Console.WriteLine(sid); // SID
var ntAccount = sid.Translate(typeof(NTAccount));
Console.WriteLine(ntAccount); // DOMAIN\username
Ustawianie właściciela wymaga połączenia do SetAccessControl aby zapisać zmiany. Ponadto nadal jesteś związany zasadami własności systemu Windows - nie możesz przypisać własności do innego konta. Możesz dać uprawnienia własności na własność i muszą one przejąć własność.
var ntAccount = new NTAccount("DOMAIN", "username");
fs.SetOwner(ntAccount);
try {
File.SetAccessControl(FILE, fs);
} catch (InvalidOperationException ex) {
Console.WriteLine("You cannot assign ownership to that user." +
"Either you don't have TakeOwnership permissions, or it is not your user account."
);
throw;
}
Zobacz również http://stackoverflow.com/questions/5241718/taking-ownership-of-files-with-broken-permissions i http://stackoverflow.com/questions/5368825/taking-ownership- of-a-file-or-folder –