本文整理汇总了C#中Microsoft.Win32.RegistryKey.GetValueKind方法的典型用法代码示例。如果您正苦于以下问题:C# RegistryKey.GetValueKind方法的具体用法?C# RegistryKey.GetValueKind怎么用?C# RegistryKey.GetValueKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Win32.RegistryKey
的用法示例。
在下文中一共展示了RegistryKey.GetValueKind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadRegistryValue
private static bool ReadRegistryValue(RegistryKey key, string valueName, bool defaultValue)
{
Debug.Assert(key != null, "'key' must not be null");
try
{
// This check will throw an IOException if keyName doesn't exist. That's OK, we return the
// default value.
if (key.GetValueKind(valueName) == RegistryValueKind.DWord)
{
// At this point we know the Registry value exists and it must be valid (any DWORD value
// can be converted to a bool).
return Convert.ToBoolean(key.GetValue(valueName), CultureInfo.InvariantCulture);
}
}
catch (UnauthorizedAccessException e)
{
LogRegistryException("ReadRegistryValue", e);
}
catch (IOException e)
{
LogRegistryException("ReadRegistryValue", e);
}
catch (SecurityException e)
{
LogRegistryException("ReadRegistryValue", e);
}
catch (ObjectDisposedException e)
{
LogRegistryException("ReadRegistryValue", e);
}
return defaultValue;
}
示例2: ReadRegistryValue
private static bool ReadRegistryValue(RegistryKey key, string valueName, bool defaultValue)
{
try
{
if (key.GetValueKind(valueName) == RegistryValueKind.DWord)
{
return Convert.ToBoolean(key.GetValue(valueName), CultureInfo.InvariantCulture);
}
}
catch (UnauthorizedAccessException exception)
{
LogRegistryException("ReadRegistryValue", exception);
}
catch (IOException exception2)
{
LogRegistryException("ReadRegistryValue", exception2);
}
catch (SecurityException exception3)
{
LogRegistryException("ReadRegistryValue", exception3);
}
catch (ObjectDisposedException exception4)
{
LogRegistryException("ReadRegistryValue", exception4);
}
return defaultValue;
}
示例3: CreateComponentValue
internal void CreateComponentValue(RegistryKey componentKey, string name, object value, RegistryValueKind kind)
{
if (null == componentKey)
throw new ArgumentNullException("Unable to find specified registry key.");
if (componentKey.GetValue(name, null) != null && componentKey.GetValueKind(name) != kind)
componentKey.DeleteValue(name, false);
componentKey.SetValue(name, value, kind);
}
示例4: RegValueEntry
/// <summary>
/// This constructor creates a named value from a Windows registry value
/// </summary>
/// <param name="key">Parent registry key</param>
/// <param name="name">Name of the value</param>
public RegValueEntry(RegistryKey key, string name)
{
Kind = MapNativeKindToRegis3Kind(key.GetValueKind(name));
Value = key.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
Name = name;
if (Value is string)
{
string temp = Value as string;
if (temp.EndsWith("\0"))
{
Value = temp.Substring(0, temp.Length - 1);
}
}
RemoveFlag = false;
}
示例5: InnerCompare
private static bool InnerCompare(RegistryKey A, RegistryKey B, string root,ref Dictionary<string, Data> output)
{
bool current = true;
try
{
if (A != null)
{
// Process A
foreach (var Name in A.GetValueNames())
{
string EntryName = root + A.Name + "::" + Name;
var dat = new Data();
dat.SetA(A.GetValue(Name), A.GetValueKind(Name));
output.Add(EntryName, dat);
}
foreach (var keyName in A.GetSubKeyNames())
{
RegistryKey subA = A.OpenSubKey(keyName);
RegistryKey subB = B == null ? null : B.OpenSubKey(keyName);
current &= InnerCompare(subA, subB, root + keyName + @"\", ref output);
}
}
if (B != null)
{
foreach (var Name in B.GetValueNames())
{
string EntryName = root + B.Name + "::" + Name;
Data dat = output.ContainsKey(EntryName) ? output[EntryName] : new Data();
dat.SetB(B.GetValue(Name), B.GetValueKind(Name));
output[EntryName] = dat;
}
foreach (var keyName in B.GetSubKeyNames())
{
// when we get here, we have already cleared everything present in the A side
RegistryKey subB = B.OpenSubKey(keyName);
current &= InnerCompare(null, subB, root + keyName + @"\", ref output);
}
}
return current;
}
catch (Exception e)
{
return false;
}
}
示例6: RecurseCopyKey
private static void RecurseCopyKey(RegistryKey sourceKey, RegistryKey destinationKey)
{
//copy all the values
foreach (string valueName in sourceKey.GetValueNames())
{
object objValue = sourceKey.GetValue(valueName);
RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
destinationKey.SetValue(valueName, objValue, valKind);
}
//For Each subKey
//Create a new subKey in destinationKey
//Call myself
foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
{
RegistryKey sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName);
RegistryKey destSubKey = destinationKey.CreateSubKey(sourceSubKeyName);
RecurseCopyKey(sourceSubKey, destSubKey);
}
}
示例7: GetRegKeyValueAsString
private static string GetRegKeyValueAsString(RegistryKey key, string valueName)
{
string str = null;
try
{
if (key.GetValueKind(valueName) == RegistryValueKind.String)
{
str = key.GetValue(valueName) as string;
}
}
catch (ArgumentException)
{
}
catch (IOException)
{
}
catch (SecurityException)
{
}
return str;
}
示例8: RecurseCopyKey
private void RecurseCopyKey(RegistryKey sourceKey, RegistryKey destinationKey)
{
//copy all the values
foreach (string valueName in sourceKey.GetValueNames())
{
object objValue = sourceKey.GetValue(valueName);
RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
destinationKey.SetValue(valueName, objValue, valKind);
}
//For Each subKey
//Create a new subKey in destinationKey
//Call myself
foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
{
RegistryKey sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
RegistryKey destSubKey = destinationKey.CreateSubKey(sourceSubKeyName,
RegistryKeyPermissionCheck.ReadWriteSubTree);
RecurseCopyKey(sourceSubKey, destSubKey);
}
}
示例9: getSession
public static Session getSession(RegistryKey registryKey)
{
var ret = new Session();
if (registryKey != null)
{
var valueKeys = registryKey.GetValueNames();
foreach (var key in valueKeys)
{
var value = new RegistryValue
{
kind = registryKey.GetValueKind(key),
key = key,
value = registryKey.GetValue(key).ToString()
};
ret.Add(value);
}
}
return ret;
}
示例10: Win32RegKeyValueKind
public static LWRegistryValueKind Win32RegKeyValueKind(RegistryKey hKey, string sValue)
{
try
{
RegistryValueKind valueKind = hKey.GetValueKind(sValue);
switch (valueKind)
{
case RegistryValueKind.Binary:
return LWRegistryValueKind.REG_BINARY;
case RegistryValueKind.String:
return LWRegistryValueKind.REG_SZ;
case RegistryValueKind.DWord:
return LWRegistryValueKind.REG_DWORD;
case RegistryValueKind.ExpandString:
return LWRegistryValueKind.REG_EXPAND_SZ;
case RegistryValueKind.Unknown:
return LWRegistryValueKind.REG_RESOURCE_LIST;
case RegistryValueKind.MultiString:
return LWRegistryValueKind.REG_MULTI_SZ;
case RegistryValueKind.QWord:
return LWRegistryValueKind.REG_QUADWORD;
default:
return LWRegistryValueKind.REG_BINARY;
}
}
catch (Exception ex)
{
Logger.LogException("Win32RegKeyValueKind : ", ex);
return LWRegistryValueKind.REG_SZ;
}
}
示例11: LoadValues
/* ----------------------------------------------------------------- */
///
/// LoadValues
///
/// <summary>
/// レジストリからデータをロードする.レジストリは,階層構造を
/// 持つ場合,Subkeys と Values に分かれるため,Values の部分
/// のみを処理する.
/// </summary>
///
/* ----------------------------------------------------------------- */
private void LoadValues(RegistryKey root, ParameterCollection dest)
{
foreach (string name in root.GetValueNames()) {
if (dest.Contains(name)) continue;
var item = new ParameterElement();
item.Key = name;
var kind = root.GetValueKind(name);
if (kind == Microsoft.Win32.RegistryValueKind.String) {
item.Type = ParameterType.String;
item.Value = root.GetValue(name, "");
}
else if (kind == Microsoft.Win32.RegistryValueKind.DWord) {
item.Type = ParameterType.Integer;
item.Value = root.GetValue(name, 0);
}
else {
throw new NotSupportedException(kind.ToString());
}
dest.Add(item);
}
}
示例12: Win32RegValueKind
public static void Win32RegValueKind(RegistryKey hKey, string sValue, SubKeyValueInfo keyValueInfo)
{
string sDataType = string.Empty;
StringBuilder sbTemp = new StringBuilder();
keyValueInfo.sData = string.Empty;
keyValueInfo.sDataBuf = null;
object sDataBuf = null;
int type;
RegistryValueKind valueKind = hKey.GetValueKind(sValue);
switch (valueKind)
{
case RegistryValueKind.Unknown:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_RESOURCE_LIST;
string[] sKey = hKey.ToString().Split(new char[] { '\\' } , 2);
sDataBuf = RegGetValue(GetRegistryHive(keyValueInfo.hKey), sKey[1], sValue, out type);
keyValueInfo.intDataType = type;
if (sDataBuf != null)
{
byte[] sBinaryData = sDataBuf as byte[];
foreach (byte byt in sBinaryData)
{
string stringValue = BitConverter.ToString(new byte[] { byt });
if (stringValue.Length == 1)
stringValue = "0" + stringValue;
sbTemp.Append(stringValue);
sbTemp.Append(" ");
}
keyValueInfo.sData = sbTemp.ToString();
}
break;
case RegistryValueKind.Binary:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_BINARY;
sDataBuf = hKey.GetValue(sValue, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (sDataBuf != null)
{
byte[] sBinaryData = sDataBuf as byte[];
if (sBinaryData != null)
{
foreach (byte byt in sBinaryData)
{
string stringValue = BitConverter.ToString(new byte[] { byt });
if (stringValue.Length == 1)
stringValue = "0" + stringValue;
sbTemp.Append(stringValue);
sbTemp.Append(" ");
}
}
}
keyValueInfo.sData = sbTemp.ToString();
break;
case RegistryValueKind.DWord:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_DWORD;
keyValueInfo.sDataBuf = hKey.GetValue(sValue, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (keyValueInfo.sDataBuf != null)
{
string sTemp = keyValueInfo.sDataBuf.ToString();
sTemp = RegistryUtils.DecimalToBase((UInt32)Convert.ToInt32(keyValueInfo.sDataBuf), 16);
keyValueInfo.sData = string.Concat("0x", sTemp.PadLeft(8,'0'), "(", ((uint)Convert.ToInt32(keyValueInfo.sDataBuf)).ToString(), ")");
}
break;
case RegistryValueKind.ExpandString:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_EXPAND_SZ;
sDataBuf = hKey.GetValue(sValue, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (sDataBuf != null)
{
keyValueInfo.sData = sDataBuf.ToString();
}
break;
case RegistryValueKind.MultiString:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_MULTI_SZ;
sDataBuf = hKey.GetValue(sValue, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (sDataBuf != null)
{
string[] sStringData = sDataBuf as string[];
if (sStringData != null)
{
foreach (string sr in sStringData)
{
sbTemp.Append(sr);
sbTemp.Append(" ");
}
}
}
keyValueInfo.sData = sbTemp.ToString();
break;
case RegistryValueKind.QWord:
keyValueInfo.RegDataType = LWRegistryValueKind.REG_QUADWORD;
sDataBuf = hKey.GetValue(sValue, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (sDataBuf != null)
{
string sTemp = sDataBuf.ToString();
sTemp = RegistryUtils.DecimalToBase((UInt64)Convert.ToInt64(sDataBuf), 16);
keyValueInfo.sData = string.Concat("0x", sTemp.PadLeft(16,'0'), "(", ((ulong)Convert.ToInt64(sDataBuf)).ToString(), ")");
}
//.........这里部分代码省略.........
示例13: FormatValueData
/// <summary>Retrieves the data from the <paramref name="valueName"/> in <paramref name="key"/>.</summary>
/// <param name="key">The registry key to get value data.</param>
/// <param name="valueName">The name of the value</param>
/// <returns>The value data as string, with type prefix if needed.</returns>
/// <remarks>The format of the data is <Value type>:<Value data>
/// Registry types not in <see cref="RegistryValueKind"/>, could be read using entry in <c>advapi32.dll</c>.
/// <para>
/// References:
/// <ul>
/// <li><see href="http://en.wikipedia.org/wiki/Windows_Registry#.REG_files">.REG files (also known as Registration entries)</see></li>
/// <li><see href="http://en.wikipedia.org/wiki/Windows_Registry#cite_ref-4">List of standard registry value types</see></li>
/// <li><see href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724884%28v=vs.85%29.aspx">Registry Value Types</see></li>
/// <li><see href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registryvaluekind.aspx">RegistryValueKind Enumeration</see></li>
/// <li><see href="http://www.dotnetframework.org/default.aspx/Dotnetfx_Vista_SP2/Dotnetfx_Vista_SP2/[email protected]@[email protected]/DEVDIV/depot/DevDiv/releases/whidbey/NetFxQFE/ndp/clr/src/BCL/Microsoft/Win32/[email protected]/1/[email protected]">RegistryKey.cs source code in C# .NET</see></li>
/// </ul>
/// </para><para>
/// <table>
/// <caption>Numeric values for Registry Predefined Value Types in <c>Winnt.h</c>.</caption>
/// <tr><th>Type</th><th>Value</th></tr>
/// <tr><td>REG_NONE</td><td>0</td></tr>
/// <tr><td>REG_SZ</td><td>1</td></tr>
/// <tr><td>REG_EXPAND_SZ</td><td>2</td></tr>
/// <tr><td>REG_BINARY</td><td>3</td></tr>
/// <tr><td>REG_DWORD</td><td>4</td></tr>
/// <tr><td>REG_DWORD_LITTLE_ENDIAN</td><td>4</td></tr>
/// <tr><td>REG_DWORD_BIG_ENDIAN</td><td>5</td></tr>
/// <tr><td>REG_LINK</td><td>6</td></tr>
/// <tr><td>REG_MULTI_SZ</td><td>7</td></tr>
/// <tr><td>REG_RESOURCE_LIST</td><td>8</td></tr>
/// <tr><td>REG_FULL_RESOURCE_DESCRIPTOR</td><td>9</td></tr>
/// <tr><td>REG_RESOURCE_REQUIREMENTS_LIST</td><td>10</td></tr>
/// <tr><td>REG_QWORD</td><td>11</td></tr>
/// <tr><td>REG_QWORD_LITTLE_ENDIAN</td><td>11</td></tr>
/// </table>
/// </para><para>
/// <table>
/// <caption>Numeric values for Registry Types in <see cref="RegistryValueKind"/> Enumeration.</caption>
/// <tr><th>Type</th><th>Value</th></tr>
/// <tr><td>None</td><td>-1</td></tr>
/// <tr><td>Unknown</td><td>0</td></tr>
/// <tr><td>String</td><td>1</td></tr>
/// <tr><td>ExpandString</td><td>2</td></tr>
/// <tr><td>Binary</td><td>3</td></tr>
/// <tr><td>DWord</td><td>4</td></tr>
/// <tr><td>MultiString</td><td>7</td></tr>
/// <tr><td>QWord</td><td>11</td></tr>
/// </table>
/// </para>
/// </remarks>
private static string FormatValueData(RegistryKey key, string valueName)
{
Contract.Requires(key != null);
Contract.Requires(valueName != null);
Contract.Ensures(Contract.Result<string>() != null);
string text = string.Empty;
RegistryValueKind rvk = key.GetValueKind(valueName);
object keyValue = key.GetValue(valueName);
if (keyValue != null)
{
switch (rvk)
{
case RegistryValueKind.None:
byte[] none = (byte[])keyValue;
text = string.Format(CultureInfo.InvariantCulture, "hex(0):{0}", ByteToHexList(none));
break;
case RegistryValueKind.Unknown:
throw new UnauthorizedAccessException("Unknown registry value kind.");
case RegistryValueKind.String:
string value = (string)keyValue;
text = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", value.ToString().Replace(@"\", @"\\").Replace(@"""", @"\"""));
break;
case RegistryValueKind.ExpandString:
string expand = (string)keyValue;
text = string.Format(CultureInfo.InvariantCulture, "hex(2):{0}", StringToHexList(expand));
break;
case RegistryValueKind.Binary:
byte[] binary = (byte[])keyValue;
text = string.Format(CultureInfo.InvariantCulture, "hex:{0}", ByteToHexList(binary));
break;
case RegistryValueKind.DWord:
int dword = (int)keyValue;
text = string.Format(CultureInfo.InvariantCulture, "dword:{0:x8}", dword);
break;
case RegistryValueKind.MultiString:
string[] multi = (string[])keyValue;
text = string.Format(CultureInfo.InvariantCulture, "hex(7):{0}", StringArrayToHexList(multi));
break;
case RegistryValueKind.QWord:
byte[] qword = (byte[])keyValue;
text = string.Format(CultureInfo.InvariantCulture, "hex(b):{0}", ByteToHexList(qword));
break;
default:
throw new UnauthorizedAccessException("Default registry value kind.");
}
}
return text;
}
示例14: ReadValue
private static object ReadValue(RegistryKey r, string n)
{
if (r.GetValueKind(n) == RegistryValueKind.None)
return null;
return r.GetValue(n);
}
示例15: GetStringValueFromRegistry
static string GetStringValueFromRegistry(RegistryKey key, string valueName)
{
object val = key.GetValue(valueName);
if (val == null) return string.Empty;
RegistryValueKind kind = key.GetValueKind(valueName);
if (kind == RegistryValueKind.DWord ||
kind == RegistryValueKind.QWord ||
kind == RegistryValueKind.String)
{
return val.ToString();
}
else if (kind == RegistryValueKind.ExpandString)
{
return Environment.ExpandEnvironmentVariables(val as string);
}
else if (kind == RegistryValueKind.MultiString)
{
StringBuilder buf = new StringBuilder();
foreach (var s in val as string[])
{
buf.AppendFormat("{0}, ", s);
}
return buf.ToString();
}
else if (kind == RegistryValueKind.Binary)
{
StringBuilder buf = new StringBuilder();
foreach (var b in val as byte[])
{
buf.AppendFormat("{0:x2} ", b);
}
if (buf.Length > 0) buf.Remove(buf.Length - 1, 1);
return buf.ToString();
}
else return string.Empty;
}