Win32.RegGetValueExp C# (CSharp) Method

RegGetValueExp() static public method

static public RegGetValueExp ( uint key, string subKey, string valName ) : string
key uint
subKey string
valName string
return string
    static public string RegGetValueExp(uint key, string subKey, string valName)
    {
        //this method is required in order to rtreive REG_EXPAND_SZ registry value
        const int KEY_READ = 0x00000001;
        int hkey = 0;
        try
        {
            if (0 == Win32.RegOpenKeyEx(key, subKey, 0, KEY_READ, out hkey))
            {
                StringBuilder sb = new StringBuilder(1024 * 10);
                int lpcbData = sb.Capacity;
                uint lpType;
                if (0 == RegQueryValueEx(hkey, valName, 0, out lpType, sb, ref lpcbData))
                    return sb.ToString();
            }
        }
        finally
        {
            if (0 != hkey)
                RegCloseKey(hkey);
        }
        return null;
    }
    static public int RegSetStrValue(uint key, string subKey, string valName, string val)

Usage Example

Example #1
0
    static public void UnInstall()
    {
        Action <string> deleteFile = (file) => { try { if (File.Exists(file))
                                                       {
                                                           File.Delete(file);
                                                       }
                                                 } catch { } };

        try
        {
            string oldHomeDir = Environment.GetEnvironmentVariable("CSSCRIPT_DIR");

            if (CSScriptInstaller.IsComShellExtInstalled() && File.Exists(CSScriptInstaller.comShellEtxDLL32))
            {
                CSScriptInstaller.UninstallComShellExt();
            }

            string path = "";
            using (RegistryKey envVars = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", true))
            {
                path = Win32.RegGetValueExp(Win32.HKEY_LOCAL_MACHINE, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path");
                RemoveFromPath(@"%CSSCRIPT_DIR%", path);
                envVars.DeleteValue("CSSCRIPT_DIR", false);
            }

            path = RemoveFromPath(@"%CSSCRIPT_DIR%", path);
            path = RemoveFromPath(@"%CSSCRIPT_DIR%\lib", path);
            Win32.RegSetStrValue(Win32.HKEY_LOCAL_MACHINE, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", path);

            DeleteKey("CsScript");
            DeleteKey(@".cs\ShellNew");
            DeleteKey(@".ccs");

            //restore the original file type
            CopyKeyValue(".cs", "pre_css_default", "");
            CopyKeyValue(".cs", "pre_css_contenttype", "Content Type");

            DeleteKeyValue(".cs", "pre_css_default");
            DeleteKeyValue(".cs", "pre_css_contenttype");

            using (RegistryKey csKey = GetKey(".cs", true))
                if (csKey.GetValue("OldDefault") != null)
                {
                    csKey.SetValue("", csKey.GetValue("OldDefault").ToString());
                }

            deleteFile(Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\cscs.exe.config"));
            deleteFile(Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\csws.exe.config"));
            deleteFile(Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\css_config.xml"));

            SplashScreen.HideSplash();
            MessageBox.Show("CS-Script has been deactivated.\nPlease note that some files may still be locked by external applications (e.g. Windows Explorer)", "CS-Script Configuration");
        }
        catch (Exception e)
        {
            MessageBox.Show("Cannot perform uninstall operation.\n" + e.ToString(), "CS-Script Configuration");
        }
    }