Пример #1
0
 public static void WriteRegistryValue <T>(RegistryKey key, string name, T value)
 {
     if (value != null)
     {
         key.SetValue(name, InvariantConverter.ToString(value));
     }
     else
     {
         key.SetValue(name, "");
     }
 }
Пример #2
0
 public static T DeserializeValueFromRegistry <T>(RegistryKey key, string name, T defaultValue)
 {
     try
     {
         string strValue = (string)key.GetValue(name, InvariantConverter.ToString(defaultValue));
         return(DeserializeValue <T>(strValue));
     }
     catch
     {
         return(defaultValue);
     }
 }
Пример #3
0
 public static void WriteRegistryList <T>(RegistryKey key, string baseName, IEnumerable <T> list)
 {
     if (list != null)
     {
         int index = 1;
         foreach (T item in list)
         {
             Utils.WriteRegistryValue(key, baseName + index.ToString(), InvariantConverter.ToString(item));
             ++index;
         }
     }
 }
Пример #4
0
 public static T ReadRegistryValue <T>(RegistryKey key, string name, T defaultValue)
 {
     try
     {
         string strValue = (string)key.GetValue(name, InvariantConverter.ToString(defaultValue));
         return(InvariantConverter.FromString <T>(strValue));
     }
     catch
     {
         return(defaultValue);
     }
 }
Пример #5
0
 public static IEnumerable <T> ReadRegistryList <T>(RegistryKey key, string baseName)
 {
     for (int index = 1; ; ++index)
     {
         object value = key.GetValue(baseName + index.ToString());
         if (value != null)
         {
             yield return(InvariantConverter.FromString <T>(value.ToString()));
         }
         else
         {
             break;
         }
     }
 }
Пример #6
0
        public static void Load(object data, RegistryKey key)
        {
            foreach (var propertyInfo in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                RegistrySaveAttribute valueAttr = GetAttribute <RegistrySaveAttribute>(propertyInfo);
                if (valueAttr != null)
                {
                    if (valueAttr.IsObject)
                    {
                        RegistryKey subkey = key.OpenSubKey(valueAttr.Name);
                        if (subkey != null)
                        {
                            // Try to get an existing value of the property.
                            object value = propertyInfo.GetValue(data, null);

                            // In the property has no value, and the subkey exists, create a new value object.
                            if (value == null)
                            {
                                value = Activator.CreateInstance(propertyInfo.PropertyType);
                                propertyInfo.SetValue(data, value, null);
                            }

                            // Load the value members (no matter whether the value existed or has been created).
                            Load(value, subkey);

                            // Close the subkey.
                            subkey.Close();
                        }
                        else
                        {
                            // If there's no subkey, erase the object.
                            propertyInfo.SetValue(data, null, null);
                        }
                    }
                    else
                    {
                        // Load the simple value.
                        string strValue = key.GetValue(valueAttr.Name) as string;
                        object value    = strValue != null?InvariantConverter.FromString(strValue, propertyInfo.PropertyType) : valueAttr.DefaultValue;

                        propertyInfo.SetValue(data, value, null);
                    }
                }
            }
        }
Пример #7
0
        private static void Save(object data, RegistryKey root, string keyName)
        {
            RegistryKey currentKey = root.CreateSubKey(keyName);

            if (currentKey != null)
            {
                foreach (var propertyInfo in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    RegistrySaveAttribute valueAttr = GetAttribute <RegistrySaveAttribute>(propertyInfo);
                    if (valueAttr != null)
                    {
                        object value = propertyInfo.GetValue(data, null);

                        if (valueAttr.IsObject)
                        {
                            if (value == null)
                            {
                                root.DeleteSubKey(keyName + '\\' + valueAttr.Name);
                            }
                            else
                            {
                                Save(value, root, keyName + '\\' + valueAttr.Name);
                            }
                        }
                        else
                        {
                            currentKey.SetValue(valueAttr.Name, InvariantConverter.ToString(value));
                        }

                        //if (propertyInfo.PropertyType.GetInterface(IList.GetType().ToString()) != null)
                    }
                }

                // All done.
                currentKey.Close();
            }
        }
Пример #8
0
 /// <summary>
 /// Converts a value to string using <ref>InvariantConverter</ref>
 /// </summary>
 /// <param name="value">The value to convert.</param>
 /// <returns>The invariant string representation of the value.</returns>
 public static string ToInvString <T>(this T value)
 {
     return(InvariantConverter.ToString(value));
 }