public void GetOwnershipTest() { // Arrange var tmpDir = Path.Combine(Path.GetTempPath(), "dirtools-test-" + Guid.NewGuid().ToString()); var localSystem = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null); var dirSec = new DirectorySecurity(); dirSec.SetOwner(localSystem); Directory.CreateDirectory(tmpDir, dirSec); // Act var curIdentity = new NTAccount(Environment.UserDomainName, Environment.UserName); DirectoryTools.GetOwnershipForDirectory(tmpDir, curIdentity); // Assert var curDirsec = new DirectorySecurity(tmpDir, AccessControlSections.Owner); IdentityReference owner = curDirsec.GetOwner(typeof(NTAccount)); Assert.IsTrue(curIdentity == owner); }
private string getFolderOwner(string path) { DirectorySecurity tmp = new DirectorySecurity(path, AccessControlSections.Owner); string owner = "<unknown>"; try { owner = tmp.GetOwner(System.Type.GetType("System.Security.Principal.SecurityIdentifier")).Value; } catch{} return owner; }