6

My .NET Core library needs to read some info from registry if it's available or leave the default value if it's not. I would like to get the idea of best practices of doing that.

I think I could wrap the registry initialization/usage block in try/catch or I can check if the current platform is Windows but I don't think these are best practices (it's better to avoid exceptions and there is no guarantee that ANY Windows-based platform will have the registry, etc).

For now, I will rely on

bool hasRegistry = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

but wondering if there is a more reliable/universal solution.

Alex
  • 2,469
  • 3
  • 28
  • 61
  • Isn't the only way into the Registry via Microsoft.Win32 ? This will not support other operating systems in an abstracted way, its always specific to Windows. – Alex K. Dec 08 '16 at 16:26
  • you should most definietely check the OS version first. See here on how to do it in .NET core (for now) https://stackoverflow.com/questions/38790802/determine-operating-system-in-net-core – d.moncada Dec 08 '16 at 16:28
  • @AlexK. What if MS releases some Windows version (let's say, Windows for coffee machines) which does not have registry. I know how to check if it's Windows or not but I'm not sure that whenever I have Windows, I can always count on Registry. It can be true today but "tomorrow never knows".. – Alex Dec 08 '16 at 16:41
  • What kind of data are you going to try and look for in the Windows registry? Would a non-Windows specific solution such as environment variables be an option? – Justin Saraceno Dec 08 '16 at 18:27
  • In that particular case, I need to access MIME database to match file extensions to content-types. But it's just one particular case. I may need to access other platform-specific functionality as well. For instance, get info from WMI, from network interfaces, and much more. So I'm looking for some unified way to check if the classes needed are available in a particular runtime. – Alex Dec 09 '16 at 09:41
  • @sproketboy Are you sure I can use Java from .NET Core app? I don't think it's possible, at least without building a very special environment (I can't tell my customers "Hey, you'll also need to install Java on your box"). – Alex May 13 '17 at 16:36
  • @sproketboy Your opinion isn't valuable to anyone. – Bailey Miller Oct 16 '17 at 13:12
  • @sproketboy I am just saying this is a place to answer questions not out right state that the user's framework of choice is the root problem. You understand that you are not contributing to helping at all? – Bailey Miller Oct 19 '17 at 20:57

1 Answers1

6

Checking for registry with RuntimeInformation.IsOSPlatform(OSPlatform.Windows) is sufficient.

If some kind of Windows will not have registry (as you noted in comments), it will most probably have new OSPlatform property anyway...

You can use Microsoft's Windows Compatibility Pack for reading registry. Check their example...

private static string GetLoggingPath()
{
    // Verify the code is running on Windows.
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Fabrikam\AssetManagement"))
        {
            if (key?.GetValue("LoggingDirectoryPath") is string configuredPath)
                return configuredPath;
        }
    }

    // This is either not running on Windows or no logging path was configured,
    // so just use the path for non-roaming user-specific data files.
    var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    return Path.Combine(appDataPath, "Fabrikam", "AssetManagement", "Logging");
}
Matěj Pokorný
  • 16,977
  • 5
  • 39
  • 48