I wrote a code that wonderfully recognizes the current owner of the file, but doesn’t want to change it to any other. Judging by MSDN, everything is spelled out as it should. But does not work.
Here is the code:
using System; using System.IO; using System.Security.Principal; namespace GET_SET_OWNER { class Program { public static bool GetOwner(string File_UNC) { Console.WriteLine("Текущий владелец:"); var File_Security = File.GetAccessControl(File_UNC); var SID = File_Security.GetOwner(typeof(SecurityIdentifier)); Console.WriteLine(SID); var Owner = SID.Translate(typeof(NTAccount)); Console.WriteLine(Owner); return true; } public static bool SetOwner(string File_UNC) { Console.WriteLine(Environment.NewLine + "Задаем нового владельца..." + Environment.NewLine); var New_Owner = new NTAccount("Home", "Браток"); var File_Security = File.GetAccessControl(File_UNC); File_Security.SetOwner(New_Owner); try { File.SetAccessControl(File_UNC, File_Security); } catch (InvalidOperationException) { return false; } return true; } static void Main(string[] args) { const string File_UNC = @"D:\1.txt"; bool res_1 = false, res_2 = false, res_3 = false; res_1 = GetOwner(File_UNC); res_2 = SetOwner(File_UNC); res_3 = GetOwner(File_UNC); Console.WriteLine("{0} {1} {2}", res_1, res_2, res_3); Console.ReadLine(); } } } Does anyone have any ideas? The SetOwner function on each attempt to change the current owner issues System.InvalidOperationException: The security identifier is not allowed to own this object and returns false . In this case, if you change the owner of the file to the current one, that is, for example, (User) is the current owner, change to (User), the function does its work and returns true .
In the case where the current owner of the file is the Matrix , the program copes with the task and changes the owner to any other in the system. That is, changing the owner from Brother to User is not a problem. The problem is to carry out an operation to change the owner from User to Brother .
SetOwnerreturn? - VladDfalseortrue, are we allowed to guess? - VladD