public RegEdit(HKEY hkey, string address) { HKEY = hkey; Address = address; _regkey = new RegKey(); _regvalue = new RegValue(); }
public static bool CreateVolatileRegistryKey(HKEY baseKey, string subkey) { const int REG_OPTION_VOLATILE = 1; const int KEY_CREATE_SUB_KEY = 4; IntPtr handle; int disposition = 0; int errorCode = Utilities.RegCreateKeyEx(new IntPtr((int)baseKey), subkey, 0, null, REG_OPTION_VOLATILE, KEY_CREATE_SUB_KEY, IntPtr.Zero, out handle, out disposition); if (errorCode == 0) { Utilities.RegCloseKey(handle); } else { throw new Exception("Error creating registry key"); } const int REG_CREATED_NEW_KEY = 1; return(disposition == REG_CREATED_NEW_KEY); }
//创建指定的键 public static int CreateKey(HKEY Root, string SubKey, string KeyName) { RegistryKey subKey = reg[(int)Root]; if (KeyName.Length == 0) { return(2); } try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.CreateSubKey(strKeyName); } } subKey.CreateSubKey(KeyName); subKey.Close(); return(0); } catch { return(1); } }
//读指定变量值 public static string ReadValue(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) { return("[ERROR]"); } try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } string strKey = subKey.GetValue(ValueName).ToString(); subKey.Close(); return(strKey); } catch { return("[ERROR]"); } }
//判断指定的键是否存在 public static int IsExistKey(HKEY Root, string SubKey, string KeyName) { RegistryKey subKey = reg[(int)Root]; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } string[] strSubKey1 = subKey.GetSubKeyNames(); foreach (string strKeyName in strSubKey1) { if (strKeyName == KeyName) { return(0); } } return(1); } catch { return(2); } }
//写指定变量值 public static int WriteValue(HKEY Root, string SubKey, string ValueName, object ValueData, RegistryValueKind ValueType) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) { return(2); } try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.CreateSubKey(strKeyName); } } subKey.SetValue(ValueName, ValueData, ValueType); subKey.Close(); return(0); } catch { return(1); } }
//读指定变量的类型 public static RegistryValueKind ReadValueType(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) { return(RegistryValueKind.Unknown); } try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } RegistryValueKind valueType = subKey.GetValueKind(ValueName); subKey.Close(); return(valueType); } catch { return(RegistryValueKind.Unknown); } }
public HRESULT RegDeleteKeyValuePrintf(HKEY hkey, string pszKeyFormatString, string pszValue, params object[] argList) { var szKeyName = string.Format(pszKeyFormatString, argList); var hr = HRESULT_FROM_WIN32(RegDeleteKeyValue(hkey, szKeyName, pszValue)); _UpdateAssocChanged(hr, pszKeyFormatString); return(MapNotFoundToSuccess(hr)); }
/// <summary> /// Gets byte value of a value in the registry. /// </summary> /// <param name="hkey"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static byte[] getByteValue(HKEY hkey, String key, String value) { var byteobj = getKeyValue(getBaseKey(hkey, false), key, value) as byte[]; if (byteobj.IsNullOrEmpty()) { byteobj = getKeyValue(getBaseKey(hkey, true), key, value) as byte[]; } return(byteobj); }
public static void WriteBool(HKEY hk, string sPath, string sName, bool bValue) { int iVal = 0; if (bValue) { iVal = 1; } WriteDWORD(hk, sPath, sName, iVal); }
public void SetRegistryKeySecurity(HKEY hKey, SECURITY_INFORMATION secInfo) { int rc = Win32.RegSetKeySecurity(hKey, secInfo, this._secDesc); if (rc != Win32.SUCCESS) { Win32.SetLastError((uint)rc); Win32.ThrowLastError(); } }
public static void WriteDWORD(HKEY hk, string sPath, string sName, int iValue) { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); store.CreateFolderPath(strFullPath); strFullPath += "\\" + sName + ".DWORD"; store.WriteTextFile(strFullPath, iValue.ToString(), true); }
public static void WriteString(HKEY hk, string sPath, string sName, string sValue) { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); store.CreateFolderPath(strFullPath); strFullPath += "\\" + sName + ".String"; store.WriteTextFile(strFullPath, sValue, true); }
public static void DeleteValue(HKEY hk, string sPath, string sName) { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); //MUST NOT!!! //store.CreateFolderPath( strFullPath ); strFullPath += "\\" + sName + ".DWORD"; store.DeleteFile(strFullPath); }
public RscRegistryValueList(HKEY hk, string sPath, string sTitle = "Item") { m_hk = hk; m_sPath = sPath; m_sTitle = sTitle; m_iCountWritten = RscRegistry.ReadDWORD(m_hk, m_sPath, sTitle + "Count", 0); for (int i = 0; i < m_iCountWritten; i++) { string sVal = RscRegistry.ReadString(m_hk, m_sPath, sTitle + i.ToString(), ""); m_a.Add(sVal); } }
public static string HKEY2String(HKEY hk) { switch (hk) { case HKEY.HKEY_CLASSES_ROOT: return("HKEY_CLASSES_ROOT"); case HKEY.HKEY_CURRENT_USER: return("HKEY_CURRENT_USER"); default: return(""); } }
static RegistryKey getBaseKey(HKEY hkey, Boolean get64) { switch (hkey) { case HKEY.CLASSES_ROOT: if (get64) { return(getBaseKey64(RegistryHive.ClassesRoot)); } return(getBaseKey32(RegistryHive.ClassesRoot)); case HKEY.CURRENT_USER: if (get64) { return(getBaseKey64(RegistryHive.CurrentUser)); } return(getBaseKey32(RegistryHive.CurrentUser)); case HKEY.LOCAL_MACHINE: if (get64) { return(getBaseKey64(RegistryHive.LocalMachine)); } return(getBaseKey32(RegistryHive.LocalMachine)); case HKEY.USERS: if (get64) { return(getBaseKey64(RegistryHive.Users)); } return(getBaseKey32(RegistryHive.Users)); case HKEY.PERFORMANCE_DATA: if (get64) { return(getBaseKey64(RegistryHive.PerformanceData)); } return(getBaseKey32(RegistryHive.PerformanceData)); case HKEY.CURRENT_CONFIG: if (get64) { return(getBaseKey64(RegistryHive.CurrentConfig)); } return(getBaseKey32(RegistryHive.CurrentConfig)); } return(null); }
public static string ReadString(HKEY hk, string sPath, string sName, string sDefaultValue = "") { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); //MUST NOT!!! //store.CreateFolderPath( strFullPath ); strFullPath += "\\" + sName + ".String"; bool bNotExist = false; return(store.ReadTextFile(strFullPath, sDefaultValue, out bNotExist)); }
public static string GetFullPath(HKEY hk, string sPath) { string strFullPath; strFullPath = RscKnownFolders.GetSystemPath("Registry"); strFullPath += "\\" + HKEY2String(hk); sPath = RscUtils.RemoveStarting(sPath, "\\"); sPath = RscUtils.RemoveEnding(sPath, "\\"); if (sPath.Length > 0) { strFullPath += "\\" + sPath; } return(strFullPath); }
public static bool ReadBool(HKEY hk, string sPath, string sName, bool bDefaultValue = false) { int iDef = 0; if (bDefaultValue) { iDef = 1; } int iVal = ReadDWORD(hk, sPath, sName, iDef); if (iVal != 0) { return(true); } return(false); }
public static void Win32RegOpenRemoteBaseKey(HKEY hKeyType, out RegistryKey Key) { RegistryKey hKey = null; string sHostname = string.Concat(@"\\", System.Environment.MachineName); sHostName = sHostname; if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { switch (hKeyType) { case HKEY.HEKY_CURRENT_USER: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, sHostname); break; case HKEY.HKEY_CLASSES_ROOT: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.ClassesRoot, sHostname); break; case HKEY.HKEY_CURRENT_CONFIG: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentConfig, sHostname); break; case HKEY.HKEY_DYN_DATA: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.DynData, sHostname); break; case HKEY.HKEY_LOCAL_MACHINE: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, sHostname); break; case HKEY.HKEY_PERFORMENCE_DATA: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.PerformanceData, sHostname); break; case HKEY.HKEY_USERS: hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, sHostname); break; default: break; } //hKey.SetAccessControl(regSecurity); } Key = hKey; }
/// <summary> /// Gets string value of a value in the registry. /// </summary> /// <param name="hkey"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static String getStringValue(HKEY hkey, String key, String value) { String text; if (CheckIf.Is64BitOS) { text = getKeyValue(getBaseKey(hkey, true), key, value).ToString(); if (text.IsNullOrEmpty()) { text = getKeyValue(getBaseKey(hkey, false), key, value).ToString(); } } else { text = getKeyValue(getBaseKey(hkey, false), key, value).ToString(); } return(text); }
public static string[] GetKeys(HKEY hk, string sPath, string sFilter = "") { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); if (!store.FolderExists(strFullPath)) { return new String[] {} } ; if (sFilter.Length == 0) { sFilter = "*.*"; } return(store.GetFolderNames(strFullPath, sFilter)); }
public static int ReadDWORD(HKEY hk, string sPath, string sName, int iDefaultValue = 0) { string strFullPath = GetFullPath(hk, sPath); RscStore store = new RscStore(); //MUST NOT!!! //store.CreateFolderPath( strFullPath ); strFullPath += "\\" + sName + ".DWORD"; bool bNotExist = false; string sValue = store.ReadTextFile(strFullPath, iDefaultValue.ToString(), out bNotExist); int iValue = iDefaultValue; Int32.TryParse(sValue, out iValue); return(iValue); }
public static RegistryHive GetRegistryHive(HKEY hKey) { RegistryHive hive; switch (hKey) { case HKEY.HKEY_CLASSES_ROOT: hive = RegistryHive.ClassesRoot; break; case HKEY.HKEY_LOCAL_MACHINE: hive = RegistryHive.LocalMachine; break; case HKEY.HEKY_CURRENT_USER: hive = RegistryHive.CurrentUser; break; case HKEY.HKEY_CURRENT_CONFIG: hive = RegistryHive.CurrentConfig; break; case HKEY.HKEY_DYN_DATA: hive = RegistryHive.DynData; break; case HKEY.HKEY_PERFORMENCE_DATA: hive = RegistryHive.PerformanceData; break; case HKEY.HKEY_USERS: hive = RegistryHive.Users; break; default: hive = RegistryHive.LocalMachine; break; } return(hive); }
//删除指定变量 public static int DeleteValue(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName, true); } } subKey.DeleteValue(ValueName, true); subKey.Close(); return 0; } catch { return 1; } }
public static SecurityDescriptor GetRegistryKeySecurity(HKEY hKey, SECURITY_INFORMATION secInfo) { DWORD cbLength = 0; int rc = Win32.RegGetKeySecurity(hKey, secInfo, IntPtr.Zero, ref cbLength); if (rc != Win32.SUCCESS) { Win32.SetLastError((uint)rc); } switch (rc) { case Win32.SUCCESS: throw new InvalidOperationException("Unexpected return code from RegGetKeySecurity()"); case Win32.ERROR_INSUFFICIENT_BUFFER: IntPtr secDescPtr = Win32.AllocGlobal(cbLength); try { rc = Win32.RegGetKeySecurity(hKey, secInfo, secDescPtr, ref cbLength); if (rc != Win32.SUCCESS) { Win32.SetLastError((uint)rc); Win32.ThrowLastError(); } return(new SecurityDescriptor(secDescPtr)); } catch { Win32.FreeGlobal(secDescPtr); throw; } default: Win32.ThrowLastError(); return(null); // Never executed } }
private string ReadValue(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) { return("[ERROR]"); } try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } string[] s = subKey.GetValueNames(); byte[] strKeyb = (byte[])subKey.GetValue(ValueName); string strKey = ""; foreach (byte b in strKeyb) { string k = Convert.ToInt16(b).ToString("X"); if (k.Length < 2) { k = "0" + k; } strKey = strKey + k; } subKey.Close(); return(strKey); } catch { return("[ERROR]"); } }
//创建指定的键 public static int CreateKey(HKEY Root, string SubKey, string KeyName) { RegistryKey subKey = reg[(int)Root]; if (KeyName.Length == 0) return 2; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.CreateSubKey(strKeyName); } } subKey.CreateSubKey(KeyName); subKey.Close(); return 0; } catch { return 1; } }
//删除指定的键 public static int DeleteKey(HKEY Root, string SubKey, string KeyName) { RegistryKey subKey = reg[(int)Root]; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName, true); } } subKey.DeleteSubKeyTree(KeyName); subKey.Close(); return(0); } catch { return(1); } }
public static SecurityDescriptor GetRegistryKeySecurity(HKEY hKey, SECURITY_INFORMATION secInfo) { DWORD cbLength = 0; int rc = Win32.RegGetKeySecurity(hKey, secInfo, IntPtr.Zero, ref cbLength); if (rc != Win32.SUCCESS) Win32.SetLastError((uint)rc); switch(rc) { case Win32.SUCCESS: throw new InvalidOperationException("Unexpected return code from RegGetKeySecurity()"); case Win32.ERROR_INSUFFICIENT_BUFFER: IntPtr secDescPtr = Win32.AllocGlobal(cbLength); try { rc = Win32.RegGetKeySecurity(hKey, secInfo, secDescPtr, ref cbLength); if (rc != Win32.SUCCESS) { Win32.SetLastError((uint)rc); Win32.ThrowLastError(); } return new SecurityDescriptor(secDescPtr); } catch { Win32.FreeGlobal(secDescPtr); throw; } default: Win32.ThrowLastError(); return null; // Never executed } }
internal void Initialize(FrameworkElement owner) { #if CMH_DEBUG (DebugTrace(XCP_TRACE_OUTPUT_MSG /*traceType*/, "CMH[0x%p]: Initialize.", this)); #endif // CMH_DEBUG FrameworkElement ownerLocal = owner; m_wpOwner = new WeakReference(ownerLocal); void OnLoadedClearFlags(object pSender, RoutedEventArgs args) { ClearStateFlags(); } owner.Loaded += OnLoadedClearFlags; m_loadedHandler = Disposable.Create(() => owner.Loaded -= OnLoadedClearFlags); #if false // Try and read from Reg Key HKEY key = null; if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop\\", 0, KEY_QUERY_VALUE, &key)) { char Buffer[32] = { 0 };
//枚举指定的键的值 public static string[] EnumValueName(HKEY Root, string SubKey) { RegistryKey subKey = reg[(int)Root]; if (SubKey.Length == 0) { return(null); } try { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } string[] strValue = subKey.GetValueNames(); subKey.Close(); return(strValue); } catch { return(null); } }
public static extern LONG RegOpenKey( HKEY hKey, LPCTSTR lpSubKey, out HKEY phkResult );
//写指定变量值 public static int WriteValue(HKEY Root, string SubKey, string ValueName, object ValueData, RegistryValueKind ValueType) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) return 2; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.CreateSubKey(strKeyName); } } subKey.SetValue(ValueName, ValueData, ValueType); subKey.Close(); return 0; } catch { return 1; } }
public static string Database(string dsn, HKEY hkey) { string source; string database = ""; string subkey = ODBCREG + "\\" + dsn; if ( hkey == HKEY.CurrentUser ) source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn); else source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn); if ( source != "Microsoft Access Driver (*.mdb)" ) { if ( hkey == HKEY.CurrentUser) database = (string)HKCU.ReadOption(subkey, "database", database); else database = (string)HKLM.ReadOption(subkey, "database", database); } else { if ( hkey == HKEY.CurrentUser) database = (string)HKCU.ReadOption(subkey, "DBQ", database); else database = (string)HKLM.ReadOption(subkey, "DBQ", database); } return database; }
private void EnumChildNodes(RegistryKey sSubKey, HKEY hKey) { Array sSubKeys = null; Array sValues = null; Dictionary<string, LACTreeNode> nodesAdded = new Dictionary<string, LACTreeNode>(); Dictionary<string, LACTreeNode> nodesToAdd = new Dictionary<string, LACTreeNode>(); foreach (LACTreeNode n in treeNode.Nodes) nodesAdded.Add(n.Text.Trim(), n); if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { if (sSubKey != null) { if (nodeType != RegistryViewerPlugin.NodeType.HKEY_SUBKEY) { SubKeyInfo subKeyInfo = new SubKeyInfo(); subKeyInfo.sKey = sSubKey.Name; subKeyInfo.hKey = hKey; subKeyInfo.sSubKey = sSubKey; treeNode.Tag = subKeyInfo; } RegistryInteropWrapperWindows.Win32RegSubKeyList(sSubKey, out sSubKeys); if (sSubKeys != null && sSubKeys.Length != 0) { foreach (string key in sSubKeys) { RegistryKey subKey = RegistryInteropWrapperWindows.Win32RegOpenRemoteSubKey(sSubKey, key); Icon ic = Properties.Resources.Reports; LACTreeNode node = Manage.CreateIconNode(key, ic, typeof(RegistryViewerKeyPage), plugin); node.sc = plugin.GetPlugInNode().sc; SubKeyInfo subKeyInfo = new SubKeyInfo(); subKeyInfo.sKey = key; subKeyInfo.hKey = hKey; subKeyInfo.sSubKey = subKey; node.Tag = subKeyInfo; if (!nodesAdded.ContainsKey(key.Trim())) nodesToAdd.Add(key, node); } } RegistryInteropWrapperWindows.Win32RegSubKeyValueList(sSubKey, out sValues); ListViewItem lvItem = new ListViewItem(new string[] { "(Default)", "REG_SZ", "(value not set)" }); SubKeyValueInfo valueInfo = new SubKeyValueInfo(); valueInfo.hKey = hKey; valueInfo.sParentKey = sSubKey; valueInfo.sValue = ""; valueInfo.sData = "(value not set)"; valueInfo.RegDataType = LWRegistryValueKind.REG_SZ; valueInfo.IsDefaultValue = true; lvItem.Tag = valueInfo; lvRegistryPage.Items.Add(lvItem); if (sValues != null && sValues.Length != 0) { foreach (string value in sValues) { valueInfo = new SubKeyValueInfo(); RegistryInteropWrapperWindows.Win32RegValueKind(sSubKey, value, valueInfo); valueInfo.hKey = hKey; valueInfo.sParentKey = sSubKey; valueInfo.sValue = value; valueInfo.IsDefaultValue = false; if (String.IsNullOrEmpty(value)) { valueInfo.IsDefaultValue = true; lvItem = new ListViewItem(new string[] { "(Default)", GetRegValueStringType(valueInfo.RegDataType), valueInfo.sData }); lvRegistryPage.Items.RemoveAt(0); } else lvItem = new ListViewItem(new string[] { value, GetRegValueStringType(valueInfo.RegDataType), valueInfo.sData }); lvItem.Tag = valueInfo; lvRegistryPage.Items.Add(lvItem); } } } } LACTreeNode[] nodestoAddedRe = new LACTreeNode[nodesToAdd.Count]; int idx = 0; if (nodesToAdd != null && nodesToAdd.Count != 0) { foreach (string key in nodesToAdd.Keys) { nodestoAddedRe[idx] = nodesToAdd[key]; idx++; } } if (nodestoAddedRe != null && nodestoAddedRe.Length != 0) treeNode.Nodes.AddRange(nodestoAddedRe); }
internal static extern int RegGetValue( HKEY hkey, [MarshalAs(UnmanagedType.LPTStr)] LPCTSTR lpSubKey, [MarshalAs(UnmanagedType.LPTStr)] LPCTSTR lpValue, DWORD dwFlags, out DWORD pdwType, StringBuilder pvData, ref DWORD pcbData);
//判断指定的键是否存在 public static int IsExistKey(HKEY Root, string SubKey, string KeyName) { RegistryKey subKey = reg[(int)Root]; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } string[] strSubKey1 = subKey.GetSubKeyNames(); foreach (string strKeyName in strSubKey1) { if (strKeyName == KeyName) return 0; } return 1; } catch { return 2; } }
public static extern LONG RegCloseKey(HKEY hKey);
public static string Server(string dsn, HKEY hkey) { string source; string server = ""; string subkey = ODBCREG + "\\" + dsn; if ( hkey == HKEY.CurrentUser) source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn); else source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn); if ( source != "Microsoft Access Driver (*.mdb)" ) { if ( hkey == HKEY.CurrentUser) server = (string)HKCU.ReadOption(subkey, "server", server); else server = (string)HKLM.ReadOption(subkey, "server", server); } return server; }
public static extern int RegCloseKey(HKEY hkey);
public static string provider(string dsn, HKEY hkey) { string source; if ( hkey == HKEY.CurrentUser) source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn); else source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn); return source; }
public static string GetConnectionStringFromDsn(string dsn, HKEY hkey) { string con; string source; string server = "localhost"; string database = "Master"; string subkey = ODBCREG + "\\" + dsn; con = "server={0};database={1};Trusted_Connection=true"; if ( hkey == HKEY.CurrentUser) source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn); else source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn); if ( source == "SQL Server" ) { if ( hkey == HKEY.CurrentUser) { server = (string)HKCU.ReadOption(subkey, "server", server); database = (string)HKCU.ReadOption(subkey, "database", database); } else { server = (string)HKLM.ReadOption(subkey, "server", server); database = (string)HKLM.ReadOption(subkey, "database", database); } return string.Format(con, server, database); } return "dsn=" + dsn; }
public static string[] DsnList(HKEY hkey) { string []odbcs; if ( hkey == HKEY.CurrentUser ) odbcs = HKCU.ValueNames(ODBC_SOURCES); else odbcs = HKLM.ValueNames(ODBC_SOURCES); return odbcs; }
//写指定变量值 public static int WriteValue(HKEY Root, string SubKey, string ValueName, string ValueData) { return WriteValue(Root, SubKey, ValueName, ValueData, RegistryValueKind.String); }
public static extern int RegOpenKeyExW(HKEY hkey, string szSubKey, int dwOptions, int samDesired, ref HKEY phkResult);
//读指定变量的类型 public static RegistryValueKind ReadValueType(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) return RegistryValueKind.Unknown; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } RegistryValueKind valueType = subKey.GetValueKind(ValueName); subKey.Close(); return valueType; } catch { return RegistryValueKind.Unknown; } }
public static extern LONG RegGetKeySecurity( HKEY hKey, // handle to key SECURITY_INFORMATION SecurityInformation, // request PSECURITY_DESCRIPTOR pSecurityDescriptor, // SD ref DWORD lpcbSecurityDescriptor // buffer size );
//读指定变量值 public static string ReadValue(HKEY Root, string SubKey, string ValueName) { RegistryKey subKey = reg[(int)Root]; if (ValueName.Length == 0) return "[ERROR]"; try { if (SubKey.Length > 0) { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } } string strKey = subKey.GetValue(ValueName).ToString(); subKey.Close(); return strKey; } catch { return "[ERROR]"; } }
public static extern LONG RegSetKeySecurity( HKEY hKey, SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor );
internal static extern int RegOpenKeyEx( HKEY hKey, [MarshalAs(UnmanagedType.LPTStr)] LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, out PHKEY phkResult);
//枚举指定的键的值 public static string[] EnumValueName(HKEY Root, string SubKey) { RegistryKey subKey = reg[(int)Root]; if (SubKey.Length == 0) return null; try { string[] strSubKey = SubKey.Split('\\'); foreach (string strKeyName in strSubKey) { subKey = subKey.OpenSubKey(strKeyName); } string[] strValue = subKey.GetValueNames(); subKey.Close(); return strValue; } catch { return null; } }
public static RegistryHive GetRegistryHive(HKEY hKey) { RegistryHive hive; switch (hKey) { case HKEY.HKEY_CLASSES_ROOT: hive = RegistryHive.ClassesRoot; break; case HKEY.HKEY_LOCAL_MACHINE: hive = RegistryHive.LocalMachine; break; case HKEY.HEKY_CURRENT_USER: hive = RegistryHive.CurrentUser; break; case HKEY.HKEY_CURRENT_CONFIG: hive = RegistryHive.CurrentConfig; break; case HKEY.HKEY_DYN_DATA: hive = RegistryHive.DynData; break; case HKEY.HKEY_PERFORMENCE_DATA: hive = RegistryHive.PerformanceData; break; case HKEY.HKEY_USERS: hive = RegistryHive.Users; break; default: hive = RegistryHive.LocalMachine; break; } return hive; }
public static extern int RegNotifyChangeKeyValue(HKEY hkey, bool bWatchSubtree, int dwNotifyFilter, HANDLE hEvent, bool fAsynchronous);