Search code examples
c#stringregistrystring-formattingregistrykey

How do I handle the whitespaces of a registry path?


I'm creating a process in my application that makes it possible to run commands like you do it in the command prompt.

When I want to export a registry key, I use REG EXPORT path file to access the registry key and export the information into the file. If I use a path which contains no whitespaces at all, everything works fine. But as soon as I use a path with whitespaces, e.g. HKEY_CURRENT_USER\Software\NVIDIA Corporation, it does not work.

I'm appending the path as a String with a StringBuilder to the command, the String itself looks like this: String path = "HKEY_CURRENT_USER\\Software\\NVIDIA Corporation".

Do I have to change the String contained in path? Or is there a special static method I can use to format the String?


Solution

  • You probably need to surround the path with quotes. This might help:

    String path = "\"HKEY_CURRENT_USER\\Software\\NVIDIA Corporation\"";