
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Granting/Revoking Access to a File or Registry Key
|
993
NTAccount constructor must be changed to a valid user or group name that exists on
your machine. If you pass in the name of an existing user or group account that has
been disabled, an
IdentityNotMappedException will be thrown with the message
“Some or all identity references could not be translated.”
The second item that is needed is either a valid
RegistryKey object, if you are modify-
ing security access to a registry key or a string containing a valid path and filename to
an existing file. These objects will have security permissions either granted to them
or revoked from them.
Once these two items have been obtained, you can use the second item to obtain a
security object, which contains the list of access-rule objects. For example, the fol-
lowing code obtains the security object for the registry key HKEY-LOCAL_
MACHINE\SOFTWARE\MyCompany\MyApp:
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\MyCompany\MyApp");
RegistrySecurity regSecurity = regKey.GetAccessControl( );
The following code obtains the security object for the FOO.TXT file:
string file = @"c:\FOO.TXT";
FileSecurity fileSecurity = File.GetAccessControl(file);
Now that you have your particular security object, you can create an access-rule
object that ...