FileAssociation/RegistryWrapper.cs
/// <summary>
/// Writes specified value to the registry.
/// </summary>
/// <param name="path">Full registry key (minus root) that will contain the value.</param>
/// <param name="valueName">Name of the value within key that will be written.</param>
/// <param name="value">Value to be written</param>
Writes specified value to the registry.
Writes specified value to the registry .
public void Write(string path, string valueName, object value)
        {
            RegistryKey key = RegistryWrapper.ClassesRoot;
            RegistryKey lastKey = key;
            string[] parts = path.Split('\\');
            if (parts == null || parts.Length == 0)
            {
                return;
            }
            for (int x = 0; x < parts.Length; x++)
            {
                key = key.OpenSubKey(parts[x], true);
                if (key == null)
                {
                    key = lastKey.CreateSubKey(parts[x]);
                }
                if (x == parts.Length - 1)
                {
                    if (value is string)
                    {
                        key.SetValue(valueName, value.ToString());
                    }
                    else if (value is uint || value.GetType().IsEnum)
                    {
                        object o = key.GetValue(valueName, null);
                        if (o == null)
                        {
                            key.SetValue(valueName, value, RegistryValueKind.DWord);
                        }
                        else
                        {
                            RegistryValueKind kind = key.GetValueKind(valueName);
                            if (kind == RegistryValueKind.DWord)
                            {
                                key.SetValue(valueName, value, RegistryValueKind.DWord);
                            }
                            else if (kind == RegistryValueKind.Binary)
                            {
                                uint num = (uint)value;
                                byte[] b = new byte[4];
                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 1);
                                b[2] = (byte)((num & 0x00FF0000) >> 2);
                                b[3] = (byte)((num & 0xFF000000) >> 3);
                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 8);
                                b[2] = (byte)((num & 0x00FF0000) >> 16);
                                b[3] = (byte)((num & 0xFF000000) >> 24);
                                key.SetValue(valueName, b, RegistryValueKind.Binary);
                            }
                            else if (kind == RegistryValueKind.String)
                            {
                                key.SetValue(valueName, "x" + ((uint)value).ToString("X8"));
                            }
                        }
                    }
                    else if (value is Guid)
                    {
                        key.SetValue(valueName, ((Guid)value).ToString("B"));
                    }
                }
                lastKey = key;
            }
            if (key != null)
                key.Close();
        }
public void Write ( string path , string valueName , object value ) { RegistryKey key = RegistryWrapper . ClassesRoot ; RegistryKey lastKey = key ; string [ ] parts = path . Split ( ' \\ ' ) ; if ( parts == null || parts . Length == 0 ) { return ; } for ( int x = 0 ; x < parts . Length ; x ++ ) { key = key . OpenSubKey ( parts [ x ] , true ) ; if ( key == null ) { key = lastKey . CreateSubKey ( parts [ x ] ) ; } if ( x == parts . Length - 1 ) { if ( value is string ) { key . SetValue ( valueName , value . ToString ( ) ) ; } else if ( value is uint || value . GetType ( ) . IsEnum ) { object o = key . GetValue ( valueName , null ) ; if ( o == null ) { key . SetValue ( valueName , value , RegistryValueKind . DWord ) ; } else { RegistryValueKind kind = key . GetValueKind ( valueName ) ; if ( kind == RegistryValueKind . DWord ) { key . SetValue ( valueName , value , RegistryValueKind . DWord ) ; } else if ( kind == RegistryValueKind . Binary ) { uint num = ( uint ) value ; byte [ ] b = new byte [ 4 ] ; b [ 0 ] = ( byte ) ( ( num & 0x000000FF ) >> 0 ) ; b [ 1 ] = ( byte ) ( ( num & 0x0000FF00 ) >> 1 ) ; b [ 2 ] = ( byte ) ( ( num & 0x00FF0000 ) >> 2 ) ; b [ 3 ] = ( byte ) ( ( num & 0xFF000000 ) >> 3 ) ; b [ 0 ] = ( byte ) ( ( num & 0x000000FF ) >> 0 ) ; b [ 1 ] = ( byte ) ( ( num & 0x0000FF00 ) >> 8 ) ; b [ 2 ] = ( byte ) ( ( num & 0x00FF0000 ) >> 16 ) ; b [ 3 ] = ( byte ) ( ( num & 0xFF000000 ) >> 24 ) ; key . SetValue ( valueName , b , RegistryValueKind . Binary ) ; } else if ( kind == RegistryValueKind . String ) { key . SetValue ( valueName , " " + ( ( uint ) value ) . ToString ( " " ) ) ; } } } else if ( value is Guid ) { key . SetValue ( valueName , ( ( Guid ) value ) . ToString ( " " ) ) ; } } lastKey = key ; } if ( key != null ) key . Close ( ) ; }
Writes specified value to the registry.
Writes specified value to the registry .
path
string
Full registry key (minus root) that will contain the value.
Full registry key ( minus root ) that will contain the value .
valueName
string
Name of the value within key that will be written.
Name of the value within key that will be written .
value
object
Value to be written
Value to be written