Writing to registry local machine error - c#

I want set default icon of some extension by C#. But this gives me error -> Security Exception was unhandled
RegistryKey FileExt = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes", RegistryKeyPermissionCheck.ReadWriteSubTree);
How can I do that?

try to run your application as administrator.

Maybe the user you are using to log in on the machine does not have privileged to access or modify the registry. try run the code with administrator account and see what happens. also if there is no user logged in the same error maybe occurs for instance check this.

I presume this is on Windows Vista or 7.
You may need to have elevated privileges to change some values in the registry. Even a user with administrative privileges will get the UAC pop up to ask permission before a program can do this.
Have a look at a question about this.
Here is a codeproject article about gaining elevated privileges.

try this method instead of yours:
public RegistryKey OpenSubKey(
string name,
bool writable
)
Maybe a true is enough ;-)
Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes", true);

Related

Cant Access Registry even with Elevated Permission

I am making a Service Program where it will change the start up type of certain service.
My Current code does not even let me open the registry for writing, even when I "Run As Admin".
Code:
ServiceKey = Registry.LocalMachine;
ServiceKey = ServiceKey.OpenSubKey(#"SYSTEM\CurrentControlSet\services\" + ServiceName, true);
ServiceKey.SetValue("Start", 2, RegistryValueKind.DWord);
However, I get this error: Requested Registry Access is not allowed.
Anyone know a solution ?
(PS. I know there are other way's I could be doing this, but it's bother me how It's not letting me Access the registry.) My program is also running as Any CPU.
The way to change a service's configuration is not to whack the registry. You use the service control manager. MSDN even has a sample program that changes a service's start type. I found this page by going to About Services, then clicking Service Configuration Programs, then Service Configuration.
Found the problem, on some registry, if you check the "Permission", you will notice that not even an admin has the permission to change the registry. You can change the permission your self but on my case, I will find a different approach to editing the service start up.

Determine if a user has permissions to edit the registry?

I'm writing a program that edits the registry using c#
I would like to check if the user has access to edit when the program loads. What is the best way to check in c#?
Each node in the registry has its own ACL (Access Credential List). There is not a single right.
Commonly, each application will have its own registry node, either in the HKLM hive or in HKCU hive, or a combination of both.
In the former case, the user has to be administrator of the computer, in the latter the user can read/write its own registry.
To check the actual access permission, you can use the RegistryKey.GetAccessControl method.
However, as stated, you have to have at least the Read permissions to call this method.
Try to open a RegistryKey with write access. If it gives you an Exception, you don't have permission.
You could probably even specifiy which Exception to a certain one which says you don't have permission.
try{
Registry.CurrentUser.OpenSubKey(#"PATH\TO\STUFF", true);
// Have write permissions.
}
catch {
// Do not have write permissions.
}
A user shouldn't have access to edit "the full registry" they should only have permissions to edit certain keys within the registry.
You would be best doing a try catch system
try{
// Do registry edit
}
catch {
Console.Log("Sorry you don't have permission to edit this key");
}
Each user is different and can have access to some parts, all parts or no parts of the registry using the windows ACL.
Have a peek at the first answer here.

Way to write on registry location

work on C# window application.I want to write on registry.i know how to write on registry.I use bellow syntax to write on registry.
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.cre.CreateSubKey("asb");
key.SetValue("asb", "Isabella");
key.Close();
But problem is i fail to write on specified location .i want to write on bellow location
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
On this location want to add string value="abc" and ValueData="efd"
If have any query plz ask.thanks in advance.
For HKCU:
string keyName = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true);
rk.SetValue("abc", "efd");
rk.Close();
For HKLM you need to do it with administrative privileges. That requires adding a manifest to your program to invoke the UAC prompt on Vista or Win7.
Writing to HKEY_LOCAL_MACHINE requires administrative privileges. And if you're running on Windows Vista or 7, it also requires process elevation, lest you run afoul of UAC (User Account Control).
The best thing is only to write to this registry key during installation (where you will have full administrative privileges). You should only read from it once your application is installed.
Save all regular settings under HKEY_CURRENT_USER. Use the Registry.CurrentUser field to do that. Or, better yet, abandon the registry altogether and save your application's settings in a config file. Visual Studio has built-in support for this, it's very simple to do from C#. The registry is no longer the recommended way of saving application state.
RegistryKey reg = Registry.LocalMachine.
OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run", true);
// set value of "abc" to "efd"
reg.SetValue("abc", "efd", RegistryValueKind.DWord);
// get value of "abc"; return 0 if value not found
string value = (string)reg.GetValue("abc", "0");

Cannot Write to the Registry under HKEY_LOCAL_MACHINE\Software

I'm writing an application that needs to create a special user account hidden from login screens and the Control Panel users applet. By writing a DWORD value of 0 with the user name to the registry key below, I'm able to accomplish this goal:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon\SpecialAccounts\UserList
The problem is that under Windows 7 with UAC on, no matter what I try, I cannot programmatically write a value to the key above.
It is my understanding that writing to certain keys this is not allowed on Windows 7 with UAC on, unless you are running with Administrative privileges. I've added an application manifest requestedExecutionLevel level="requireAdministrator" uiAccess="false", I accept the UAC prompt when my program is run, my account is a member of Administrators, yet I am still unable to write to the above registry key.
What more do I need to do? How is it possible, in any application configuration, to write keys and values under HKEY_LOCAL_MACHINE\SOFTWARE?
Further information ... When my program runs, no errors are thrown and it seems to write values. My guess is that Windows is virtualizing the location to which I am writing. I need to write to the actual location, not a virtual one, if I am to hide this special user account.
Probably the program runs as 32-bit program on the 64-bit operation system? In the case I recommend you to search the values which you created under Wow6432Node subkey of the HKEY_LOCAL_MACHINE\SOFTWARE.
You can read more about such kind of virtualization here. You can use KEY_WOW64_32KEY flag in some API to be able to work with full registry without virtualization.
Write Value to Registry
string user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.WriteKey | RegistryRights.ChangePermissions,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny));
RegistryKey rk = null;
try
{
rk = Registry.CurrentUser.CreateSubKey("SOFTWARE\\TEST",
RegistryKeyPermissionCheck.Default, rs);
rk.SetValue("NAME", "IROSH);
rk.SetValue("FROM", "SRI LANKA");
}
This could have something to do with the redirection they added in Vista. I would be curious if you tried to read that registry value from your code, if you would get back the value you were expecting. You may also want to fire up RegMon to see if you can see where the redirection may be forcing you.
RegistryKey rk = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
rk.SetValue("Name", "Value");

security exception accessing registry when the program runs as scheduled task

the following small line throws a System.Security.SecurityException: Requested registry access is not allowed:
RegistryKey _key = HKLM.OpenSubKey("path\\to\\my settings", false);
Now.. what's the point some would ask? The point is that this runs ONLY when I am logged on. The exception is thrown if the program runs as scheduled task and nobody is logged on.
the user who runs that task is local administrator
the program does not run from a network share, it is located on the local disk
I even tried setting Code Access Security
the user has the rights to log on as a batch job
I have XP SP3 with all patches applied. The program is written in C# .Net 2.0 (tested 3.5 too)
Does anyone know whats the point here?
Torsten
EDIT: see http://gist.github.com/638576
Mhhhh...it seems related to Authorization problem too. Have you tried to use the API: OpenSubKey(...., RegistryKeyPermissionCheck) to see if something change? I guess it could be related to parent key and its authorization.
Try to see: http://msdn.microsoft.com/it-it/library/microsoft.win32.registrykeypermissioncheck.aspx (in your language). I hope it could help you...
Can you adapt this
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
string isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator) ? "Yes" : "No";
to check that the process really is successfully impersonating when there's no current user?
It seems that this is a problem of this specific computer. I tested it on another workstation and it works even without administrator privileges.
I assumed this - the program did run for years without any problems... Anyway, thanks to all!

Categories