Leaders snarfblam Posted May 23, 2005 Leaders Posted May 23, 2005 I am trying to access registry keys. I don't know beforehand, however, if the user will have permission to access the keys. I have read the information in MSDN on .Net security, and it just gave me a headache. Can anyone tell me, simply, how to determine if a user has permission to access a registry key? Or a file for that matter. I tried declaring a RegistryPermission object, specifying the key I needed to access, and specifying read/write/create access, and using the Demand() method, which, to my understanding, should simply throw a SecurityException if the user does not have permission. But rather than the exception being caught, a window would pop up, explaining to the user that the app tried to do something it didn't have permission to, asked the user if he wanted to ignore the error or quit the app, and if the user clicks ignore, the rest of the function does not execute (I assume the thread is being terminated). Quote [sIGPIC]e[/sIGPIC]
bri189a Posted May 24, 2005 Posted May 24, 2005 I think the best way is to remove the security demand (that may be what is causing your pop up window...I think) and execute normally in a try...catch statement and catch the security exception and go from there and do all of your registry accessing in the try part. It looks like you were going down that road already,... and .Net security gives a lot of people headaches. Good luck. Quote
Wraith Posted May 24, 2005 Posted May 24, 2005 You cna't remove the security demand, the framework has the security required built into it and if the caller lacks those permissions an exception will be thrown. Create a RegistryPermission instance for the key you want to access. Create a try block with a specific catch for SecurityException. In the try first Demand() your permission, if it fails execution will continue in the SecurityException catch, if it succeeds then yout try block continues. Work with the key you want in the try region. This is a quick adaption of the code i'm using to access the registry, its not copy-past able since its snipped from a much longer function ut it shows the structure i'm using to get the job done. RegistryKey key = Registry.CurrentUser; string subkey = "subkey"; RegistryKey hive = null; RegistryPermission permit=null if (permit==null) { permit = new RegistryPermission( (RegistryPermissionAccess.Write|RegistryPermissionAccess.Create), (key.Name @"\"+ subkey) ); } try { permit.Demand(); try { key = hive.OpenSubKey(subkey,true); if (key==null) { key=hive.CreateSubKey(subkey); } } catch (ArgumentNullException ane){Log.WriteError("name is null",ane);} catch (ArgumentException ae){Log.WriteError("name too long",ae);} catch (ObjectDisposedException ode){Log.WriteError("key is closed",ode);} catch (SecurityException se){Log.WriteError("not permitted",se); throw;} } catch (SecurityException /*se*/) { key=null; Log.Write("Demand for permission to "+ subkey +" failed"); //Log.Write("permit = " + Environment.NewLine + // " Read ="+permit.GetPathList(RegistryPermissionAccess.Read) + Environment.NewLine+ // " Write ="+permit.GetPathList(RegistryPermissionAccess.Write) + Environment.NewLine+ // " Create="+permit.GetPathList(RegistryPermissionAccess.Create) // ); throw; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.