[原创]简单的读写注册表配置类
第三个读写配置类,使用的是注册表,由于以前没使用过注册表做为配置文件来读写,所以该类比较初级,只是按自己想法所写,有何不足请批评指教
http://xingfustar.cnblogs.com
http://xingfustar.cnblogs.com
http://xingfustar.cnblogs.com
/*----------------------------------------------------------------
* 版权:http://XingFuStar.cnblogs.com
*
* 文件名: IniConfig
* 文件功能描述: 读写注册表中的配置
*
* 作者:XingFuStar
* 日期:2007年9月14日
*
* 当前版本:V1.0.0
*
* 修改日期:
* 修改内容:
*---------------------------------------------------------------*/
/*----------------------------------------------------------------
*
* 默认注册表键为:HKEY_LOCAL_MACHINE\Software
* 示意图如下:
*
* HKEY_LOCAL_MACHINE |
* -Software | Key1 Value1
* --程序项(SoftwareName) |
* ---子项(Section) | KeyN ValueN
*
*---------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
namespace XingFuStudio.Config
{
class RegConfig
{
private string softwareName;
private bool isConfig;
RegistryKey registryKey;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="softwareName">程序名称:对应注册表中Software项下的程序项</param>
public RegConfig(string softwareName)
{
//请不要删除以下信息
//版权:http://XingFuStar.cnblogs.com
this.SoftwareName = softwareName;
}
/// <summary>
/// 程序名称:对应注册表中Software项下的程序项
/// </summary>
public string SoftwareName
{
set
{
softwareName = value;
isConfig = OnSoftwareNameChange();
}
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="section">程序项下的子项</param>
/// <param name="key">键</param>
/// <param name="value">返回的键值</param>
/// <returns>是否读取成功</returns>
public bool ReadConfig(string section, string key, ref string value)
{
bool isRead = false;
try
{
if (isConfig)
{
RegistryKey skey = this.registryKey.OpenSubKey(section);
if (skey != null)
{
value = skey.GetValue(key).ToString();
isRead = true;
}
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isRead;
}
/// <summary>
/// 写入配置
/// </summary>
/// <param name="section">程序项下的子项</param>
/// <param name="key">键</param>
/// <param name="value">写入的键值</param>
/// <returns>是否写入成功</returns>
public bool WriteConfig(string section, string key, string value)
{
bool isWrite = false;
try
{
if (isConfig)
{
RegistryKey skey = this.registryKey.OpenSubKey(section, true);
if (skey != null)
{
skey.SetValue(key, value);
isWrite = true;
}
else //如果不存在该键则创建
{
skey = this.registryKey.CreateSubKey(section);
skey.SetValue(key, value);
isWrite = true;
}
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isWrite;
}
#region 私有方法
private bool OnSoftwareNameChange()
{
bool isLoad = false;
try
{
//打开该项
registryKey = Registry.LocalMachine.OpenSubKey("Software\\" + softwareName, true);
if (registryKey == null)
{
//如果不存在则创建该项
registryKey = Registry.LocalMachine.CreateSubKey("Software\\" + softwareName);
}
isLoad = true;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isLoad;
}
#endregion
}
}
* 版权:http://XingFuStar.cnblogs.com
*
* 文件名: IniConfig
* 文件功能描述: 读写注册表中的配置
*
* 作者:XingFuStar
* 日期:2007年9月14日
*
* 当前版本:V1.0.0
*
* 修改日期:
* 修改内容:
*---------------------------------------------------------------*/
/*----------------------------------------------------------------
*
* 默认注册表键为:HKEY_LOCAL_MACHINE\Software
* 示意图如下:
*
* HKEY_LOCAL_MACHINE |
* -Software | Key1 Value1
* --程序项(SoftwareName) |
* ---子项(Section) | KeyN ValueN
*
*---------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
namespace XingFuStudio.Config
{
class RegConfig
{
private string softwareName;
private bool isConfig;
RegistryKey registryKey;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="softwareName">程序名称:对应注册表中Software项下的程序项</param>
public RegConfig(string softwareName)
{
//请不要删除以下信息
//版权:http://XingFuStar.cnblogs.com
this.SoftwareName = softwareName;
}
/// <summary>
/// 程序名称:对应注册表中Software项下的程序项
/// </summary>
public string SoftwareName
{
set
{
softwareName = value;
isConfig = OnSoftwareNameChange();
}
}
/// <summary>
/// 读取配置
/// </summary>
/// <param name="section">程序项下的子项</param>
/// <param name="key">键</param>
/// <param name="value">返回的键值</param>
/// <returns>是否读取成功</returns>
public bool ReadConfig(string section, string key, ref string value)
{
bool isRead = false;
try
{
if (isConfig)
{
RegistryKey skey = this.registryKey.OpenSubKey(section);
if (skey != null)
{
value = skey.GetValue(key).ToString();
isRead = true;
}
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isRead;
}
/// <summary>
/// 写入配置
/// </summary>
/// <param name="section">程序项下的子项</param>
/// <param name="key">键</param>
/// <param name="value">写入的键值</param>
/// <returns>是否写入成功</returns>
public bool WriteConfig(string section, string key, string value)
{
bool isWrite = false;
try
{
if (isConfig)
{
RegistryKey skey = this.registryKey.OpenSubKey(section, true);
if (skey != null)
{
skey.SetValue(key, value);
isWrite = true;
}
else //如果不存在该键则创建
{
skey = this.registryKey.CreateSubKey(section);
skey.SetValue(key, value);
isWrite = true;
}
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isWrite;
}
#region 私有方法
private bool OnSoftwareNameChange()
{
bool isLoad = false;
try
{
//打开该项
registryKey = Registry.LocalMachine.OpenSubKey("Software\\" + softwareName, true);
if (registryKey == null)
{
//如果不存在则创建该项
registryKey = Registry.LocalMachine.CreateSubKey("Software\\" + softwareName);
}
isLoad = true;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
return isLoad;
}
#endregion
}
}
http://xingfustar.cnblogs.com