/// <summary> /// Convert-back from target to source, ie from int to int? /// </summary> public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int? result = null; double numericValue = (double)value; string stringValue = StringLib.AsNonNullString(value); Console.WriteLine("NullableIntegerToValueConverter.ConvertBack, value is " + stringValue + ", value.GetType() = " + value.GetType()); //var r = ParseLib.ParseForInteger(stringValue, false); //if (!r.IsEmpty && r.IsOk) //{ // Console.WriteLine(" and r.IntegerValue is " + r.IntegerValue); // result = r.IntegerValue; //} result = (int)numericValue; return(result); }
/// <summary> /// Store the given 64-bit integer value in the Windows Registry as a DWORD value under the given subkeyPath (which is under BaseKeyPath). /// </summary> /// <param name="valueName">the Registry value-name to store it in</param> /// <param name="integer64BitValue">the value to store, which will be as a DWORD</param> /// <param name="subkeyPath">the Registry subkey to store it under, under BaseKeyPath, if non-null</param> protected void SetValue(string valueName, Int64 integer64BitValue, string subkeyPath = null) { if (String.IsNullOrWhiteSpace(valueName)) { throw new ArgumentNullException("SetValue argument valueName must not be empty."); } using (var registryKey = this.CreateSubkeyUnderBasekey(subkeyPath)) { if (registryKey != null) { registryKey.SetValue(valueName, integer64BitValue, RegistryValueKind.QWord); } else // didn't work! { Console.WriteLine("in RegistryBase.SetValue, CreateSubkeyUnderBasekey(" + StringLib.AsNonNullString(subkeyPath) + ") failed!"); } } }
/// <summary> /// Store the given integer value in the Windows Registry as a DWORD value under the given subkeyPath (which is under BaseKeyPath). /// </summary> /// <param name="valueName">the Registry value-name to store it in</param> /// <param name="integerValue">the value to store, which will be as a DWORD</param> /// <param name="subkeyPath">the Registry subkey to store it under (with BaseKeyPath prefixing it) if non-null</param> protected void SetValue(string valueName, int integerValue, string subkeyPath = null) { if (String.IsNullOrWhiteSpace(valueName)) { throw new ArgumentNullException("SetValue argument valueName must not be empty."); } using (var registryKey = this.CreateSubkeyUnderBasekey(subkeyPath)) { if (registryKey != null) { registryKey.SetValue(valueName, integerValue); } else // didn't work! { Console.WriteLine("in RegistryBase.SetValue(" + valueName + "," + integerValue.ToString() + "," + StringLib.AsNonNullString(subkeyPath) + "), CreateSubkeyUnderBasekey(" + StringLib.AsNonNullString(subkeyPath) + ") failed!"); } } //RegistryKey registryKey = null; //try //{ // string sKeyPath = BaseKeyPath + @"\" + subkeyPath; // if (this.IsSpecificToUser) // { // registryKey = Registry.CurrentUser.CreateSubKey(sKeyPath); // } // else // { // registryKey = Registry.LocalMachine.CreateSubKey(sKeyPath); // } // if (registryKey != null) // { // registryKey.SetValue(valueName, integerValue); // } // else // didn't work! // { // Console.WriteLine("in RegistryBase.SetValue, CreateSubKey of " + sKeyPath + " failed!"); // } //} //catch (Exception x) //{ // Console.WriteLine(x.GetType().ToString() + " in RegistryBase.SetValue: " + x.Message); //} //finally //{ // if (registryKey != null) // { // registryKey.Dispose(); // registryKey = null; // } //} }
/// <summary> /// Store the given key (keyName) in the Windows Registry under BaseKeyPath (under subkeyNameUnderThat if that is non-null). /// </summary> /// <param name="keyName">the Registry key to set under BaseKeyPath</param> /// <param name="subkeyNameUnderThat">the Registry subkey under keyName to store it in, if non-null. Default is null.</param> protected void SetKey(string keyName, string subkeyNameUnderThat = null) { if (String.IsNullOrWhiteSpace(keyName)) { throw new ArgumentException("SetKey argument keyName must not be empty."); } RegistryKey registryKey = null; try { string subkeyPathToUse; if (String.IsNullOrWhiteSpace(subkeyNameUnderThat)) { subkeyPathToUse = keyName; } else { subkeyPathToUse = keyName + @"\" + subkeyNameUnderThat; } registryKey = this.CreateSubkeyUnderBasekey(subkeyPathToUse); } catch (Exception x) { Console.WriteLine(x.GetType().ToString() + " in LogNutRegistry.SetKey(" + keyName + "," + StringLib.AsNonNullString(subkeyNameUnderThat) + "): " + x.Message); } finally { if (registryKey != null) { registryKey.Dispose(); } } }
/// <summary> /// Retrieve the indicated double-precision numeric value from the Windows Registry, from under BaseKeyPath (and under subkeyPath if that is non-null). /// If the retrieval fails then the given default value is returned. /// </summary> /// <param name="valueName">the name of the Registry value to retrieve it from</param> /// <param name="doubleDefaultValue">the default value to return if the retrieval fails</param> /// <param name="subkeyPath">the Registry subkey (under BaseKeyPath) under which to retrieve it from, if non-null. Default is null.</param> /// <returns>The value from the Registry, or the default value if there's a failure</returns> protected double GetDouble(string valueName, double doubleDefaultValue, string subkeyPath = null) { double result = doubleDefaultValue; using (var registryKey = this.OpenSubkeyUnderBasekey(false, subkeyPath)) { if (registryKey != null) { string stringValue = (string)registryKey.GetValue(valueName, String.Empty); if (!String.IsNullOrEmpty(stringValue)) { if (!Double.TryParse(stringValue, out result)) { // Looks like an invalid string. Let's whine about this. Console.WriteLine("in RegistryBase.GetDouble, invalid value found: \"" + stringValue + "\""); } } } else { Console.WriteLine("in RegistryBase.GetDouble(" + valueName + ",," + StringLib.AsNonNullString(subkeyPath) + "), access failed so returning the default value."); } } return(result); }
/// <summary> /// Retrieve the indicated 64-bit integer value from the Windows Registry, from under BaseKeyPath (and under subkeyPath if that is non-null). /// If the retrieval fails then the provided default value is returned. /// </summary> /// <param name="valueName">the name of the Registry value to retrieve it from</param> /// <param name="int64DefaultValue">the default value to return if the retrieval fails</param> /// <param name="subkeyPath">the Registry subkey (under BaseKeyPath) under which to retrieve it from, if non-null. Default is null.</param> /// <returns>The value from the Registry, or the default value if there's a failure</returns> protected Int64 GetInt64(string valueName, Int64 int64DefaultValue, string subkeyPath = null) { Int64 result = int64DefaultValue; using (var registryKey = this.OpenSubkeyUnderBasekey(false, subkeyPath)) { if (registryKey != null) { result = (Int64)registryKey.GetValue(valueName, int64DefaultValue); } else { Console.WriteLine("in RegistryBase.GetInt64(" + valueName + ",," + StringLib.AsNonNullString(subkeyPath) + "), access failed so returning the default value."); } } return(result); }
/// <summary> /// Retrieve the indicated integer value from the Windows Registry, from under BaseKeyPath (and under subkeyPath if that is non-null). /// If the value that is in the Registry is a STRING, delete that and replace it with a DWORD value containing the given integerDefaultValue. /// If the retrieval fails then the provided default value is returned. /// </summary> /// <param name="valueName">the name of the Registry value to retrieve it from</param> /// <param name="integerDefaultValue">the default value to return if the retrieval fails</param> /// <param name="subkeyPath">the Registry subkey (under BaseKeyPath) under which to retrieve it from, if non-null. Default is null.</param> /// <returns>The value from the Registry, or the default value if there's a failure</returns> protected int GetAndEnsureIsInteger(string valueName, int integerDefaultValue, string subkeyPath = null) { int result = integerDefaultValue; if (String.IsNullOrWhiteSpace(valueName)) { throw new ArgumentException("for GetAndEnsureIsInteger, argument valueName must not be empty."); } //TODO: Actually, should only need write access if we need to replace it. using (var registryKey = this.OpenSubkeyUnderBasekey(true, subkeyPath)) { if (registryKey != null) { // First check to see whether a value is there. object o = registryKey.GetValue(valueName, null); if (o != null) { // There's a value. Find out whether it's a DWORD or a STRING. RegistryValueKind kind = registryKey.GetValueKind(valueName); if (kind == RegistryValueKind.DWord) { //TODO: We shouldn't have to do multipile accesses of the Registry. jh result = (int)registryKey.GetValue(valueName, integerDefaultValue); } else if (kind == RegistryValueKind.String) { // It's a STRING, so replace it with a DWORD value containing the default value. try { DeleteValue(valueName, subkeyPath); } catch (UnauthorizedAccessException) { Console.WriteLine("in RegistryBase.GetAndEnsureIsInteger(" + valueName + "," + integerDefaultValue + "," + StringLib.AsNonNullString(subkeyPath) + "), UnauthorizedAccessException trying to delete the string value."); } SetValue(valueName, integerDefaultValue, subkeyPath); } else { Console.WriteLine("in RegistryBase.GetAndEnsureIsInteger(" + valueName + "," + integerDefaultValue + "," + StringLib.AsNonNullString(subkeyPath) + "), the RegistryValueKind is " + kind); } } } else { Console.WriteLine("in RegistryBase.GetAndEnsureIsInteger(" + valueName + "," + integerDefaultValue + "," + StringLib.AsNonNullString(subkeyPath) + "), the key was not found."); } } return(result); }