I'm doing an application with a very basic little module of self-protection at the level of user security but I do not know how to take away the permission of the current user who does not write in the folder but if the application does, for example: to eliminate the autoprotección the program writes in the configuration file the status of checkbox
of self-protection, if the checkbox
is enabled the permissions will be activated, if it is disabled by the user from the interface of the program the permissions will be disabled. I got a code but at the time of writing in the program it removes the permissions until the application.
private void HAutoproteccion_OnChange(object sender, EventArgs e){
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configFile.AppSettings.Settings["Check3"].Value = Convert.ToString(HAutoproteccion.Checked);
configFile.Save(ConfigurationSaveMode.Modified);
Autoproteccion();
}
public void autoproteccion(){
if (HAutoproteccion.Checked == true){
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"
+ System.Environment.UserDomainName.ToString() + "'");
// Ve los usuarios habilitados
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
string userName= System.Security.Principal.WindowsIdentity.GetCurrent().Name;
foreach (ManagementObject mObject in mSearcher.Get()){
userName = userName.Substring(userName.LastIndexOf("\") + 1);
if (mObject["Name"].ToString() == userName){
DirectoryInfo myDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\" + userName;
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
}
} else {
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"
+ System.Environment.UserDomainName.ToString() + "'");
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
foreach (ManagementObject mObject in mSearcher.Get()){
userName = userName.Substring(userName.LastIndexOf("\") + 1);
if (mObject["Name"].ToString() == userName){
DirectoryInfo myDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\" + userName;
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write, AccessControlType.Allow));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
}
}
}
The point is that without the permissions write perfect but with the permissions not even the application lets write, some help?