如何更正确地检查RegistryKey编辑的权限?现在我编写这样的代码:
RegistryKey regKey = null;
try {
regKey = Registry.LocalMachine.OpenSubKey("Software", true);
}
catch (SecurityException ex) {
Console.WriteLine("SecurityException: {0}", ex.Message);
}
catch (Exception ex) {
Console.WriteLine("Exception: {0}", ex.Message);
}
if (null == regKey)
Console.WriteLine("Registry key not exists, or you have not necessary permission for edit it.");
else
Console.WriteLine("Registry key successfully opened. You have necessary permission for edit it.");但异常对性能影响很大。我可以不试/不接就检查一下吗?
向你问好,安德烈
发布于 2012-10-08 07:35:01
你查过RegistryPermission类了吗?在任何注册表操作之前,您应该调查您是否实际拥有该权限。类似于:
try
{
var permission = new RegistryPermission(RegistryPermissionAccess.Read, @"PATH\TO\YOUR\KEY");
permission.Demand();
// your code here
}
catch (System.Security.SecurityException ex)
{
// Handle exc
}但是,在成功的情况下,如果缺少权限,则会出现异常。
发布于 2012-10-08 07:58:22
但异常对性能影响很大。
你描述过了吗?我无法想象,除了一个非常人为的情况,这将是重要的。
发布于 2012-10-08 08:06:55
您应该只在初始化时读取注册表设置。不要在每个方法中读取注册表设置。那么,异常的性能惩罚不应该对整个程序产生明显的影响。
https://stackoverflow.com/questions/12777068
复制相似问题