我试图在安装过程中授予对我的应用程序的注册表设置对所有人或所有用户的写入权限。使用.NET设置注册表项写入权限
我的应用程序在安装后没有直接安装适当的权限,无需管理员授予他们即使键和值存在,他们不能更新?我有下面的代码片段,但由于未经授权的访问/访问被拒绝,安装程序失败。我认为我在正确的轨道上...
如何解决权限问题而无需手动注意?有更好的方法吗?我试图用Visual Studio安装程序替换一个额外的安装程序,通过添加此功能。
protected void GrantAllAccessPermission(String key)
{
try
{
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount;
// Get ACL from Windows, allow writing to the registry key
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(key, true))
{
RegistrySecurity rs = new RegistrySecurity();
// Creating registry access rule for 'Everyone' NT account
RegistryAccessRule rar = new RegistryAccessRule(
account.ToString(),
RegistryRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
rs.AddAccessRule(rar);
rk.SetAccessControl(rs);
}
}
catch (System.Security.SecurityException ex)
{
throw new InstallException(
String.Format("An exception in GrantAllAccessPermission, security exception! {0}", key),
ex);
}
catch (UnauthorizedAccessException ex)
{
throw new InstallException(
String.Format("An exception in GrantAllAccessPermission, access denied! {0}", key),
ex);
}
}
我用你的代码片段来完成类似的东西...只是让你知道,'Registry.LocalMachine.OpenSubKey(key)'应该是'Registry.LocalMachine.OpenSubKey(key,true)'(其中'true'表示该键可写)。没有改变,我跑进了'UnauthorizedAccessExceptions'。谢谢! – Pwninstein 2011-03-14 22:10:35
谢谢你,Pwninstein!它为我节省了无数时间的无奈! – 2015-01-21 21:44:37