Help others and share!

One of the most difficult issues I find, is when an application requires admin rights because the developers did not code their programs to run without admin rights. If there is a way that a program could accomplish the same required task without needing administrative privileges, it should do so. While AppSense can help you remove administrative rights from the users while elevating specific processes, developers should fix their code which is the root of the problem to begin with.

I recently needed to read the HKLM registry keys while running as the current user. I did not need write access in my scenario.

Utilizing the Microsoft Developer Library, I came across this nifty function:

Registry.GetValue Method (String, String, Object)

Testing my code, it worked great, except that I received a UAC prompt, which was not something I wanted users to deal with. I will admit, the lazy side of me was tempted to simply use AppSense Application Manager. Alas, I could not bring myself to be that lazy. Upon further research, I came across this code which worked great with the added benefit of a great example in reading 32/64 bit keys:

How to read the 64 bit registry from a 32 bit application or vice versa

using Microsoft.Win32;
 
namespace Read64bitRegistryFrom32bitApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string value64 = string.Empty;
            string value32 = string.Empty;
 
            RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
            localKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            if (localKey != null)
            {
                value64 = localKey.GetValue("RegisteredOrganization").ToString();
            }
            RegistryKey localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
            localKey32 = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            if (localKey32 != null)
            {
                value32 = localKey32.GetValue("RegisteredOrganization").ToString();
            }
        }
    }
}

 



Help others and share!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.