Registry Editing
-
I am trying a program to add keys, however i am getting an exception saying
**System.UnauthorizedAccessException : Cannot write to registry key**
You can consider me beginner in registry editing issue.using System;
using System.Security.Permissions ;
using Microsoft.Win32;public class RegistryEditor
{
public static void Main()
{
try
{
RegistryKey hkcu = Registry.CurrentUser;
RegistryKey hkSoftware = hkcu.OpenSubKey("Software");
RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft");
RegistryKey hkIshaan = hkSoftware.CreateSubKey("Test1");
hkIshaan.SetValue("Name", "Ishaan");
hkIshaan.SetValue("Age", "20");
hkIshaan.Close();
hkSoftware.Close();
hkcu.Close();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}Banking establishment are more dangerous then standing armies.
-
I am trying a program to add keys, however i am getting an exception saying
**System.UnauthorizedAccessException : Cannot write to registry key**
You can consider me beginner in registry editing issue.using System;
using System.Security.Permissions ;
using Microsoft.Win32;public class RegistryEditor
{
public static void Main()
{
try
{
RegistryKey hkcu = Registry.CurrentUser;
RegistryKey hkSoftware = hkcu.OpenSubKey("Software");
RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft");
RegistryKey hkIshaan = hkSoftware.CreateSubKey("Test1");
hkIshaan.SetValue("Name", "Ishaan");
hkIshaan.SetValue("Age", "20");
hkIshaan.Close();
hkSoftware.Close();
hkcu.Close();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}Banking establishment are more dangerous then standing armies.
Two things: 1) You can concatenate your two
OpenSubKey
calls:RegistryKey hkMicrosoft = hkcu.OpenSubKey(@"Software\\Microsoft");
- The OpenSubkey has 4 overloads. If you look it up on MSDN the last one allows you to specify the
RegistryKeyPermissionCheck
(what you can do with subkeys), andRegistryRights
(read only, read/write TakeOwnership etc.). I am still on XP, so I don't know how this applies under Vista and Weven, which I know are a bit more picky about these sorts of things. Hope that this is of some help! :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
- The OpenSubkey has 4 overloads. If you look it up on MSDN the last one allows you to specify the
-
Two things: 1) You can concatenate your two
OpenSubKey
calls:RegistryKey hkMicrosoft = hkcu.OpenSubKey(@"Software\\Microsoft");
- The OpenSubkey has 4 overloads. If you look it up on MSDN the last one allows you to specify the
RegistryKeyPermissionCheck
(what you can do with subkeys), andRegistryRights
(read only, read/write TakeOwnership etc.). I am still on XP, so I don't know how this applies under Vista and Weven, which I know are a bit more picky about these sorts of things. Hope that this is of some help! :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Thank You :) :) :) . I am also using XP. Is my rest of the code right?:confused::confused::confused:
- The OpenSubkey has 4 overloads. If you look it up on MSDN the last one allows you to specify the
-
Thank You :) :) :) . I am also using XP. Is my rest of the code right?:confused::confused::confused:
The only thing that I would say is, don't use the Microsoft subkey for writing to. Create your own subkey under Software for messing about with IshaanTesting or something like that. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”