0

I am trying to save the path of an application to a variable by using the registry.

What i want to achieve is:

1) Check if this application has an entry in the registry? (if it was installed or not)

2) If yes, I want to save the path to a variable which I can use later to use a program which is located in this path

So far i got

    public void Run1()
    {
        Console.WriteLine("Reading OCR path from registry");
        string keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
        string valueName = "OCR_path";
        string OCRPath = Microsoft.Win32.Registry.GetValue(keyName, valueName, null) as string;
        if (string.IsNullOrWhiteSpace(OCRPath))
        {
            string text = "3";
            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\OCR-Toolkit-Check.txt", text);
        }
    }
fabian09
  • 111
  • 2
  • 11
  • 1
    Possible duplicate of [How to read value of a registry key c#](https://stackoverflow.com/questions/18232972/how-to-read-value-of-a-registry-key-c-sharp) – Sinatr May 17 '18 at 11:13
  • You aren't saving value of `GetValue`, do you? – Sinatr May 17 '18 at 11:14
  • I want to save the keyName to a variable. I thought i need GetValue to get the path before i can save it – fabian09 May 17 '18 at 11:16
  • See [SetValue](https://msdn.microsoft.com/en-us/library/5a5t63w8(v=vs.110).aspx) method. – Sinatr May 17 '18 at 12:14
  • So i need to use the SetValue instead of the GetValue? – fabian09 May 17 '18 at 12:26
  • To *write into registry* - yes, to *check if registry contains that value* or *to read previously written value* - nope, you still have to use `GetValue`, refer to [this](https://stackoverflow.com/q/4276138/1997232) and duplicate. – Sinatr May 17 '18 at 12:31
  • I used a different approach. Now it works. The path is saved in OCRPath – fabian09 May 18 '18 at 05:39
  • consider to add your solution as an [answer](https://stackoverflow.com/help/self-answer) for future readers. – Sinatr May 18 '18 at 07:37

3 Answers3

0
    Console.WriteLine("Reading OCR path from Registry:");
    string keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
    string valueName = "OCR_path";
    if (Microsoft.Win32.Registry.GetValue(keyName, valueName, null) != null)
    {
        object variable = Microsoft.Win32.Registry.GetValue(keyName, valueName, null);
    }

You might want to do the null-check, after reading it to a variable. Besides, you can cast the object variable to string afterwards.

stl
  • 63
  • 1
  • 11
0

Holy moly, do not hardcode "Wow6432Node". You can get away with that on a 64 bit system opening the registry in 64-bit mode, but if you open the registry in 32-bit mode it will create a horrid thing you don't want to see. Also if you have a 32-bit OS, there is not supposed to be a "Wow6432Node" folder so you will end up creating stuff in places you shouldn't.

If you don't require opening the registry using only privileges and can rely on the permissions of the user to create/open/read keys then Microsoft already has Microsoft.Win32.Registry to help you.

string sPath = null;
RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey appKey = hklm.OpenSubKey(@"SOFTWARE\Tomcat\Common");

if(appKey != null)
{
    object oPath = appKey.GetValue("OCR_path", null);
    if(oPath != null && oPath is string)
    {
        sPath = oPath.ToString();
    }
}
C Sharp Conner
  • 378
  • 2
  • 11
  • 1
    If i understand you right your approach will cover both cases 32 Bit and 64 Bit systems right? – fabian09 May 18 '18 at 09:00
  • That is correct, if you wanted to open the registry in 64 bit mode in this case you would need to use the enum value RegistryView.Registry64. I've found that 32 bit applications on a Windows 64 bit system try to open the registry in 32 bit mode by default. As for your permissions, are you sure every user who uses this program will have access to those keys? Normally if you want user-by-user basis you instead use the HKCU ( HKEY CURRENT USER ) which is their mounted ntuser.dat file in their profile. – C Sharp Conner May 18 '18 at 13:53
  • This code will be part of automated tests which will always run in admin mode... so the access to those keys is always given. – fabian09 May 22 '18 at 06:26
  • Perfect. As long as permissions are granted for the account you should have no problems using my answer. Which I recommend you mark as the answer because the others don't deal with Wow6432Node correctly, if at all. – C Sharp Conner May 22 '18 at 14:48
0

I used a different approach and it works now. The path is saved into the variable OCRPath

    public void Run1()
{
    Console.WriteLine("Reading OCR path from registry");
    string keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
    string valueName = "OCR_path";
    string OCRPath = Microsoft.Win32.Registry.GetValue(keyName, valueName, null) as string;
    if (string.IsNullOrWhiteSpace(OCRPath))
    {
        string text = "3";
        System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\OCR-Toolkit-Check.txt", text);
    }
}
fabian09
  • 111
  • 2
  • 11