设置注册表权限
using Microsoft.Win32;
using System.Security.AccessControl;
using System.Security.Principal;
public static string Get_Reg_Val(string GroupName, string ValueName, string DefaultVal)
{try
{
string sValue = "";
try
{
sValue = (Registry.GetValue(strRegFullPath + GroupName,
ValueName, "")).ToString();
if (sValue == "")
throw new Exception();
}
catch(Exception)
{
RegistrySecurity userSecurity = new RegistrySecurity();
RegistryAccessRule userRule = new RegistryAccessRule("Everyone",
RegistryRights.FullControl,
AccessControlType.Allow);
userSecurity.AddAccessRule(userRule);
//if Registry key not set, create key and set default value
RegistryKey newKeyPass = Registry.LocalMachine.CreateSubKey(strRegLMPath + GroupName,
RegistryKeyPermissionCheck.ReadWriteSubTree,
userSecurity);
newKeyPass.SetValue(ValueName, DefaultVal);
sValue = DefaultVal;
}
return sValue;
}
catch (Exception ex)
{
return ex.Message;
}
}