locked
C# Read Remote Registry RRS feed

  • Question

  • I'm trying to access a remote machines registry and read a list of items from the uninstall folder on the machine. This code below seems to work but returns a security error when trying to access some machines. For some machines I'll sucessfully retreive the full list of items, others I'll get a security exception "Requested registry access is not allowed".

    I'm an intern working on a software package and I've requested higher rights on the network to see if that would fix this problem but that dosen't seem to help. How do I give the application the required level of security to read the remote machines registry?

    // Create a RegistryKey, which will access the HKEY_LOCAL_MACHINE
    
    public void createReg(string machineName)
    
    {
    
    // key in the registry of this machine.
    
    RegistryKey rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName);
    
    //RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
    
    // send the Installed software to database 
    
    getInstalledSoftware(rk);
    
    }
    
    //updates tables in database with the new run version of a given machine by 
    
    //searching its registry and copying text into appropriate fields
    
    public void getInstalledSoftware(RegistryKey rkey)
    
    {
    
    RegistryKey registryKey;
    
    try
    
    {
    
    registryKey = rkey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
    
    }
    
    catch (Exception e)
    
    {
    
    //display message and exit
    
    MessageBox.Show("Error: " + e.Message);
    
    return;
    
    }
    
    }
    
    

     

    • Moved by edhickey Monday, August 8, 2011 2:25 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Monday, August 8, 2011 5:33 AM

Answers

  • Hi Caleb,

    I reproed your issue on my side, and found the following elements can make your operations fail, please pay attention to these:

    1. First I check if program/application is installed on my local computer. I found I need run my Visual Studio as Administrator.
    2. Second I try this on remote computer, we need make sure the "Remote Registry" service start on both of computers, and then don't forget make the Windows Firewall on the target computer allows your Remote operations.
    3. Make sure you have enough Permissions in your target computer.
    4. Make sure the name of your target computer is correct.

    I hope this can help you.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jackie-Sun Thursday, August 11, 2011 7:38 AM
    • Marked as answer by Caleb Taplin Sunday, August 14, 2011 11:24 PM
    Thursday, August 11, 2011 7:37 AM

All replies

  • Hi Caleb,

    Thanks for your post.

    Scaning your code, that's seems OK. And I think this problem occurs because the user account that you used to log on does not have sufficient permissions. Please read this KB from Microsoft: http://support.microsoft.com/kb/842795 It may help you.

    And you also could try to use  RegistryKey.OpenRemoteBaseKey Method which opens a new RegistryKey that represents the requested key on a remote machine.

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 9, 2011 8:21 AM
  • I'm currently using the Registrykey.OpenRemoteBaseKey Method and what is so confusing is that its working 100% fine for some machines. I'm not currently using WMI but I'm wondering if I do, is there a way to incorporate the following using WMI?

    public void getInstalledSoftware(string machineName)
    
      {
    
       RegistryKey registryKey;
    
       try
    
       {
    
        RegistryKey rKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName);
    
        registryKey = rKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
    
       }
    
       catch (Exception e)
    
       {
    
        //display message and exit
    
        MessageBox.Show("Error: " + e.Message);
    
        return;
    
       }
    
        //Checksum for total number for software items read from registry
    
        int icountSoftware = 0;
    
        int machineRunVersion;
    
        DateTime dateWritten = DateTime.Now;
    
        machineRunVersion = getMaxRunVersion(sMachineName);
    
    
    
    
    
        //retreives the values of registry items 
    
        //and assigns them a variable name to be referenced by
    
        foreach (string key in registryKey.GetSubKeyNames())
    
        {
    
         icountSoftware++;
    
         RegistryKey activeKey;
    
         try
    
         {
    
          activeKey = registryKey.OpenSubKey(key);
    
         }
    
         catch (IOException e)
    
         {
    
          Console.WriteLine("{0}: {1}",
    
           e.GetType().Name, e.Message);
    
          return;
    
         }
    
    
    
         string currentUser = tbCurrentUser.Text;
    
         string machine = tbRequiredMachine.Text;
    
         string keyID = (string)activeKey.GetValue("keyID");
    
         if (keyID == null)
    
         {
    
          keyID = key;
    
         }
    
         string displayName = (string)activeKey.GetValue("DisplayName");
    
         if (displayName == null)
    
         {
    
          continue;
    
         }
    
         string installLocation = (string)activeKey.GetValue("InstallLocation");
    
    
    
         if (installLocation == null)
    
         {
    
          installLocation = "";
    
         }
    
         string displayVersion = (string)activeKey.GetValue("DisplayVersion");
    
         if (displayVersion == null)
    
         {
    
          displayVersion = "";
    
         }
    
         string contact = (string)activeKey.GetValue("Contact");
    
         if (contact == null)
    
         {
    
          contact = "";
    
         }
    
         string helpTelephone = (string)activeKey.GetValue("HelpTelephone");
    
         if (helpTelephone == null)
    
         {
    
          helpTelephone = "";
    
         }
    
         string installDate = (string)activeKey.GetValue("InstallDate");
    
         if (installDate == null)
    
         {
    
          installDate = "";
    
         }
    
         string publisher = (string)activeKey.GetValue("Publisher");
    
    
    
         if (publisher == null || publisher.Equals(""))
    
         {
    
          publisher = "Unknown Publisher";
    
         }
    
         string helpLink = (string)activeKey.GetValue("HelpLink");
    
         if (helpLink == null)
    
         {
    
          helpLink = "";
    
         }
    
          
    
         //store procedure to insert regSoftware to db
    
         string setRegSoftware = "fe_i_RegSoftware";
    
         //sql inserts
    
         SqlCommand cmd10 = new SqlCommand(setRegSoftware, sqlCon);
    
         cmd10.CommandType = CommandType.StoredProcedure;
    
         cmd10.Parameters.Add(new SqlParameter("@DisplayName", displayName));
    
         cmd10.Parameters.Add(new SqlParameter("@InstallLocation", installLocation));
    
         cmd10.Parameters.Add(new SqlParameter("@DisplayVersion", displayVersion));
    
         cmd10.Parameters.Add(new SqlParameter("@InstallDate", installDate));
    
         cmd10.Parameters.Add(new SqlParameter("@PublisherName", publisher));
    
         cmd10.Parameters.Add(new SqlParameter("@HelpLink", helpLink));
    
         cmd10.Parameters.Add(new SqlParameter("@DateRun", dateWritten));
    
         cmd10.Parameters.Add(new SqlParameter("@SerialKey", keyID));
    
         cmd10.Parameters.Add(new SqlParameter("@Contact", contact));
    
         cmd10.Parameters.Add(new SqlParameter("@HelpTelephone", helpTelephone));
    
         cmd10.Parameters.Add(new SqlParameter("@MachineName", machine));
    
         cmd10.Parameters.Add(new SqlParameter("@CurrentUser", currentUser));
    
         cmd10.Parameters.Add(new SqlParameter("@RunVersionID", machineRunVersion));
    
    
    
         SqlDataAdapter adp = new SqlDataAdapter(cmd10);
    
         DataTable dt = new DataTable(); adp.Fill(dt);
    
         dgTest.DataSource = dt;
    
        }
    
        MessageBox.Show("Software update complete");
    
       
    
       
    
      }
    
    

    I've tried asking the administrators to test this application and to give me tempory privledges to check if that was the problem and the same error seems to occur. "Insufficient access". 

    Even if I have to prompt a message and use inputs for user/password each time they open the application I'd like to somhow authenticate the users and use thier credentials to run the registry query. If anyone has helpful links to WMI tutorials or how to get a list of installed software items from Software\Microsoft\Windows\CurrentVersion\Uninstall I'd be very greatful.


    Thursday, August 11, 2011 6:07 AM
  • Hi Caleb,

    I reproed your issue on my side, and found the following elements can make your operations fail, please pay attention to these:

    1. First I check if program/application is installed on my local computer. I found I need run my Visual Studio as Administrator.
    2. Second I try this on remote computer, we need make sure the "Remote Registry" service start on both of computers, and then don't forget make the Windows Firewall on the target computer allows your Remote operations.
    3. Make sure you have enough Permissions in your target computer.
    4. Make sure the name of your target computer is correct.

    I hope this can help you.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jackie-Sun Thursday, August 11, 2011 7:38 AM
    • Marked as answer by Caleb Taplin Sunday, August 14, 2011 11:24 PM
    Thursday, August 11, 2011 7:37 AM
  • The windows firewall is turned off by default for computers within the domain, the remote registry seems to be a possible issue, I'll have a look at some of the standard builds of machines and see if its turned on.

    I know it would almost defeat the purpose of turning it off but is there a way to turn it on remotly if you have the rights/permissions?

    Sunday, August 14, 2011 10:54 PM