I am trying to implement a security on a single file to prevent either accessing the file or deleting it here is the code :
//Create file
FileStream oFileStreamDec = new FileStream(@"C:\Decrypted_AMS.cfg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oFileStreamDec.Write(DecryptedXML, 0, DecryptedXML.Length);
//Create access rules
FileSystemAccessRule oAccessRuleFullControl = new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, AccessControlType.Allow);
//Create file security and apply rules to it
FileSecurity oFileSecurity = new FileSecurity(@"C:\Decrypted_AMS.cfg", AccessControlSections.All);
oFileSecurity.AddAccessRule(oAccessRuleFullControl);
//Here is the problem !!!!!!!
oFileStreamDec.SetAccessControl(oFileSecurity);
oFileStreamDec.Close();
I have tried to open the file again after closing the stream and setting the access control but same problem occurred, i have tried also it on a normal file other than a file stream and the same too, I have an admin account with all permissions so What is the problem and how to Solver it ?