Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Registry Editing

Registry Editing

Scheduled Pinned Locked Moved C#
windows-adminsecurityhelplearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarkSorrow38
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • D DarkSorrow38

      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.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      Two things: 1) You can concatenate your two OpenSubKey calls:

      RegistryKey hkMicrosoft = hkcu.OpenSubKey(@"Software\\Microsoft");
      
      1. 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), and RegistryRights (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.”

      D 1 Reply Last reply
      0
      • H Henry Minute

        Two things: 1) You can concatenate your two OpenSubKey calls:

        RegistryKey hkMicrosoft = hkcu.OpenSubKey(@"Software\\Microsoft");
        
        1. 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), and RegistryRights (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.”

        D Offline
        D Offline
        DarkSorrow38
        wrote on last edited by
        #3

        Thank You :) :) :) . I am also using XP. Is my rest of the code right?:confused::confused::confused:

        H 1 Reply Last reply
        0
        • D DarkSorrow38

          Thank You :) :) :) . I am also using XP. Is my rest of the code right?:confused::confused::confused:

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          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.”

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups