Store, retrieve and delete settings in C# Registry My code writes, reads, and removes program settings in the registry. I'm using Visual Studio 2019 and my target Framework is .NET Framework 4.8.
I'm looking for more elegant functions.
Is there error handling I missed in these functions?
How can I optimize the code?
I have the following functions at the moment:
public static class Settings
{

    public static void SaveSetting(string sCompanyName, string sAppName, string sSection, string sValueName, string sValue)
    {
        RegistryKey softKey = Registry.CurrentUser.OpenSubKey(&quot;Software&quot;, true);
        RegistryKey compKey = softKey.CreateSubKey(sCompanyName);
        RegistryKey appKey = compKey.CreateSubKey(sAppName);
        RegistryKey secKey = appKey.CreateSubKey(sSection);
        secKey.SetValue(sValueName, sValue);
    }

    public static string GetSetting(string sCompanyName, string sAppName, string sSection, string sValueName, string sDefault)
    {
      
        RegistryKey softKey = Registry.CurrentUser.OpenSubKey(&quot;Software&quot;, true);
        RegistryKey compKey = softKey.CreateSubKey(sCompanyName);
        RegistryKey appKey = compKey.CreateSubKey(sAppName);
        RegistryKey secKey = appKey.CreateSubKey(sSection);

        return (string)secKey.GetValue(sValueName, sDefault);
    }

    public static void DeleteSetting(string sCompanyName, string sAppName, string sSection, string sValueName)
    {
        RegistryKey softKey = Registry.CurrentUser.OpenSubKey(&quot;Software&quot;, true);
        RegistryKey compKey = softKey.CreateSubKey(sCompanyName);
        RegistryKey appKey = compKey.CreateSubKey(sAppName);
        RegistryKey secKey = appKey.CreateSubKey(sSection);

        if (secKey.GetValue(sValueName) != null)
        {
            secKey.DeleteValue(sValueName);
        }

    }

}
Quick remarks:

When you're copy-pasting code, you should realize you're doing the wrong thing. The first four lines of each of your methods are exactly the same, and thus should become a method that returns a RegistryKey.

Do not needlessly abbreviate. softKey, compKey, appKey and secKey are all unclear.

Do not use Hungarian notation. Even if the type isn't clear from a parameter's name, your IDE will tell you what type it is.

Why doesn't DeleteSetting use GetSetting to retrieve the value?