using Microsoft.Win32;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Web;
|
|
namespace VueWebApi.Tools
|
{
|
public class Regedit
|
{
|
private static RegistryKey LoadRoot(string path, bool writable)
|
{
|
RegistryKey result;
|
try
|
{
|
RegistryKey registryKey = Regedit.LoadRoot(writable);
|
registryKey = registryKey.OpenSubKey(path, writable);
|
if (null == registryKey)
|
{
|
registryKey = Registry.LocalMachine.CreateSubKey(path);
|
}
|
result = registryKey;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return result;
|
}
|
|
private static RegistryKey LoadRoot(bool writable)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Registry.LocalMachine.OpenSubKey("Software", writable);
|
RegistryKey registryKey2 = registryKey.OpenSubKey("YYBB", writable);
|
if (null == registryKey2)
|
{
|
registryKey2 = registryKey.CreateSubKey("YYBB");
|
}
|
result = registryKey2;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
private static RegistryKey LoadRegistryKey(bool writable)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Registry.LocalMachine.OpenSubKey("Software", writable);
|
RegistryKey registryKey2 = registryKey.OpenSubKey("YYBB", writable);
|
if (null == registryKey2)
|
{
|
registryKey2 = registryKey.CreateSubKey("YYBB");
|
}
|
RegistryKey registryKey3 = registryKey2.OpenSubKey("connectionString", writable);
|
if (null == registryKey3)
|
{
|
registryKey3 = registryKey2.CreateSubKey("connectionString");
|
}
|
result = registryKey3;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static RegistryKey AddFolder(string itemName)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(true);
|
result = registryKey.CreateSubKey(itemName);
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool SetFolderValueX(string path, string name, string val)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(path, true);
|
registryKey.SetValue(name, val);
|
registryKey.Flush();
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool SetFolderValue(string itemName, string name, string itemValue)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.AddFolder(itemName);
|
registryKey.SetValue(name, itemValue);
|
registryKey.Flush();
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool DeleteFolder(string itemName)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(true);
|
registryKey.DeleteSubKey(itemName);
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool DeleteFolder(string path, string itemName)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(path, true);
|
registryKey.DeleteSubKey(itemName);
|
registryKey.Flush();
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool DeleteFolderValue(string itemName, string name)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(true);
|
registryKey = Regedit.LoadRoot(true);
|
RegistryKey registryKey2 = registryKey.OpenSubKey(itemName, true);
|
registryKey2.DeleteValue(name);
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static RegistryKey GetFolder(string itemName)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(false);
|
RegistryKey registryKey2 = registryKey.OpenSubKey(itemName, false);
|
if (null == registryKey2)
|
{
|
throw new Exception("不存在[" + itemName + "]的项...");
|
}
|
result = registryKey2;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static string[] GetFolderNames(string itemName)
|
{
|
RegistryKey registryKey = null;
|
string[] result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(false);
|
RegistryKey registryKey2 = registryKey.OpenSubKey(itemName, false);
|
if (null == registryKey2)
|
{
|
result = null;
|
}
|
else
|
{
|
result = registryKey2.GetValueNames();
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static string GetFolderValueX(string path, string name)
|
{
|
RegistryKey registryKey = null;
|
string result;
|
try
|
{
|
registryKey = Regedit.LoadRoot(path, false);
|
string text = string.Empty;
|
object value = registryKey.GetValue(name);
|
if (null != value)
|
{
|
text = value.ToString();
|
result = text;
|
}
|
else
|
{
|
result = null;
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static string GetFolderValue(string itemName, string name)
|
{
|
RegistryKey registryKey = null;
|
string result;
|
try
|
{
|
registryKey = Regedit.GetFolder(itemName);
|
string text = string.Empty;
|
object value = registryKey.GetValue(name);
|
if (null != value)
|
{
|
text = value.ToString();
|
result = text;
|
}
|
else
|
{
|
result = null;
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static RegistryKey CreateItem(string itemName)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Regedit.LoadRegistryKey(true);
|
result = registryKey.CreateSubKey(itemName);
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static RegistryKey CreateItem(string path, string itemName)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Regedit.AddFolder(path);
|
result = registryKey.CreateSubKey(itemName);
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool DeleteItem(string itemName)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.LoadRegistryKey(true);
|
registryKey.DeleteSubKey(itemName);
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static RegistryKey GetItem(string itemName)
|
{
|
RegistryKey registryKey = null;
|
RegistryKey result;
|
try
|
{
|
registryKey = Regedit.LoadRegistryKey(false);
|
RegistryKey registryKey2 = registryKey.OpenSubKey(itemName, false);
|
if (null == registryKey2)
|
{
|
throw new Exception("不存在[" + itemName + "]的项...");
|
}
|
result = registryKey2;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static bool SetConnectionString(string name, string connectionString)
|
{
|
return Regedit.SetValue(name, "connectionString", connectionString);
|
}
|
|
public static bool SetValue(string itemName, string name, string itemValue)
|
{
|
RegistryKey registryKey = null;
|
bool result;
|
try
|
{
|
registryKey = Regedit.CreateItem(itemName);
|
registryKey.SetValue(name, Encrypt.EncryptStr(itemValue, "fengyi", "fengyibi"));
|
registryKey.Flush();
|
result = true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static string GetConnectionString(string serverName)
|
{
|
string[] connectionLists = Regedit.GetConnectionLists();
|
string[] array = connectionLists;
|
for (int i = 0; i < array.Length; i++)
|
{
|
string a = array[i];
|
if (a == serverName)
|
{
|
return Regedit.GetValue(serverName, "connectionString", false);
|
}
|
}
|
throw new Exception("不存在配置名为[" + serverName + "]的设置,请检查!");
|
}
|
|
public static string GetValue(string itemName, string name)
|
{
|
return Regedit.GetValue(itemName, name, true);
|
}
|
|
public static string GetValue(string itemName, string name, bool isDecrpt)
|
{
|
RegistryKey registryKey = null;
|
string result;
|
try
|
{
|
registryKey = Regedit.GetItem(itemName);
|
string text = string.Empty;
|
object value = registryKey.GetValue(name);
|
if (null == value)
|
{
|
throw new Exception(itemName + "中不存在[" + name + "]项!");
|
}
|
text = value.ToString();
|
if (isDecrpt)
|
{
|
result = Encrypt.DecryptStr(text, "fengyi", "fengyibi");
|
}
|
else
|
{
|
result = text;
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return result;
|
}
|
|
public static string[] GetConnectionLists()
|
{
|
RegistryKey registryKey = null;
|
string[] subKeyNames;
|
try
|
{
|
registryKey = Regedit.LoadRegistryKey(false);
|
subKeyNames = registryKey.GetSubKeyNames();
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
if (null != registryKey)
|
{
|
registryKey.Close();
|
}
|
}
|
return subKeyNames;
|
}
|
}
|
}
|