使用
RegistryManager rwRgs = new RegistryManager() ;
private void button4_Click( object sender, System.EventArgs e)
{
string str1=rwRgs.ReadSettings("myName") ;
if(str1!="")
MessageBox.Show(str1) ;
}
private void button5_Click( object sender, System.EventArgs e)
{
if(rwRgs.SaveSettings("myName","furenjun"+System.DateTime.Now.ToString()))
MessageBox.Show("save Success") ;
}
private void button6_Click( object sender, System.EventArgs e)
{
if(rwRgs.DeleteValue("myName"))
MessageBox.Show("del Success") ;
}
private void button7_Click( object sender, System.EventArgs e)
{
if(rwRgs.CountValue() >0)
MessageBox.Show (rwRgs.CountValue().ToString() );
}
private void button8_Click( object sender, System.EventArgs e)
{
string[] str1=rwRgs.GetSubKeyValueNames();
str1=rwRgs.GetMySubKeyNames();
if(str1!=null)
{
int i=str1.Length;
MessageBox.Show(i.ToString() ) ;
string s1="";
for(int j=0;j<i;j++)
{
s1+=str1[j]+" ";
}
MessageBox.Show(s1.ToString() ) ;
}
}
private void button9_Click( object sender, System.EventArgs e)
{
MessageBox.Show ( Application.ProductName);
}
RegistryManager rwRgs = new RegistryManager() ;
private void button4_Click( object sender, System.EventArgs e)
{
string str1=rwRgs.ReadSettings("myName") ;
if(str1!="")
MessageBox.Show(str1) ;
}
private void button5_Click( object sender, System.EventArgs e)
{
if(rwRgs.SaveSettings("myName","furenjun"+System.DateTime.Now.ToString()))
MessageBox.Show("save Success") ;
}
private void button6_Click( object sender, System.EventArgs e)
{
if(rwRgs.DeleteValue("myName"))
MessageBox.Show("del Success") ;
}
private void button7_Click( object sender, System.EventArgs e)
{
if(rwRgs.CountValue() >0)
MessageBox.Show (rwRgs.CountValue().ToString() );
}
private void button8_Click( object sender, System.EventArgs e)
{
string[] str1=rwRgs.GetSubKeyValueNames();
str1=rwRgs.GetMySubKeyNames();
if(str1!=null)
{
int i=str1.Length;
MessageBox.Show(i.ToString() ) ;
string s1="";
for(int j=0;j<i;j++)
{
s1+=str1[j]+" ";
}
MessageBox.Show(s1.ToString() ) ;
}
}
private void button9_Click( object sender, System.EventArgs e)
{
MessageBox.Show ( Application.ProductName);
}
using
System;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ReadWriteRegiest
{
/**//// <summary>
/// RegistryManager 的摘要说明。
/// </summary>
public class RegistryManager
{
public RegistryManager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string myKeyStr="frj";
string mySubKeyStr="frjChild";
public string myKey
{
get{return myKeyStr;}
set{myKeyStr=value;}
}
public string mySubKey
{
get{return mySubKeyStr;}
set{mySubKeyStr=value;}
}
/**//// <summary>
/// 保存键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <param name="mySetValue"></param>
/// <returns></returns>
public bool SaveSettings(string mySetValueName,string mySetValue)
{
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.CreateSubKey(myKey) ;
RegistryKey frjSubKey=frjKey.CreateSubKey(mySubKey) ;
try
{
frjSubKey.SetValue(mySetValueName,mySetValue) ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"保存键值错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return false;
}
frjSubKey.Close();
frjKey.Close();
return true;
}
/**//// <summary>
/// 读键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <returns></returns>
public string ReadSettings(string mySetValueName)
{
string ReadValueStr="";
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return ReadValueStr;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return ReadValueStr;
}
ReadValueStr=frjSubKey.GetValue(mySetValueName).ToString() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"读注册表错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
}
frjSubKey.Close();
frjKey.Close();
return ReadValueStr;
}
/**//// <summary>
/// 删除键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <returns></returns>
public bool DeleteValue(string mySetValueName)
{
// RegistryKey MyReg = Registry.CurrentUser.OpenSubKey
// ("SOFTWARE\\SomeCompany\\SomeApp\\SomeVer", true);
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return false;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return false;
}
//frjSubKey.DeleteSubKeyTree(mySubKey) ; //递归删除子项和任何子级子项。字符串 subKey 不区分大小写。
// 备注
//必须具有适当权限才能删除子项及其目录树。
//警告 删除特定的项将移除目录树中该项下的所有项,并且不提供任何警告。
//如果想仅在子项没有子级子项时才删除该子项,请改用 DeleteSubKey。
frjSubKey.DeleteValue(mySetValueName,true);
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"删除键值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return false;
}
frjSubKey.Close();
frjKey.Close();
return true;
}
/**//// <summary>
/// 检索项中值的计数
/// </summary>
/// <returns></returns>
public int CountValue( )
{
int i=0;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return i;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return i;
}
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return 0;
}
i=frjSubKey.ValueCount;
frjSubKey.Close();
frjKey.Close();
return i;
}
/**//// <summary>
/// 检索与此项值关联的所有值名称的字符串数组
/// </summary>
/// <returns></returns>
public string[] GetSubKeyValueNames()
{
string[] ValueNames=null;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return ValueNames;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
ValueNames=frjSubKey.GetValueNames() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
/**//// <summary>
/// To obtain a list of all the subkeys (child keys) that exist within your present RegistryKey object
/// </summary>
/// <returns></returns>
public string[] GetMySubKeyNames()
{
string[] mySubKeyNames=null;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return mySubKeyNames;
}
mySubKeyNames=frjKey.GetSubKeyNames();
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
//mySubKeyNames=frjSubKey.GetSubKeyNames() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
public bool AutoRunProgram()
{
string Pname=Application.ProductName;
string ProgramNameAndPath= Application.StartupPath+@"\"+Pname+".exe";
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
try
{
if(softwareKey==null)
{
//如果子键不存在则创建之
softwareKey=Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
string[] CheckPname=softwareKey.GetValueNames();
int i=CheckPname.Length;
// MessageBox.Show(i.ToString() ) ;
bool find=false;
int j=0;
while(j<i && !find)
{
if(CheckPname[j]==Pname)
{
find=true;
// MessageBox.Show(j.ToString() +" "+CheckPname[j] );
}
j++;
}
if(!find)
{
//在注册表中设置自动启动程序
softwareKey.SetValue(Pname, ProgramNameAndPath) ;
}
else
{
if(softwareKey.GetValue(Pname).ToString() !=ProgramNameAndPath )
softwareKey.SetValue(Pname, ProgramNameAndPath) ;
// else
// MessageBox.Show("已经有了") ;
}
}
catch
{
softwareKey.Close();
return false;
}
softwareKey.Close();
return true;
}
}
}
using System.Windows.Forms;
using Microsoft.Win32;
namespace ReadWriteRegiest
{
/**//// <summary>
/// RegistryManager 的摘要说明。
/// </summary>
public class RegistryManager
{
public RegistryManager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string myKeyStr="frj";
string mySubKeyStr="frjChild";
public string myKey
{
get{return myKeyStr;}
set{myKeyStr=value;}
}
public string mySubKey
{
get{return mySubKeyStr;}
set{mySubKeyStr=value;}
}
/**//// <summary>
/// 保存键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <param name="mySetValue"></param>
/// <returns></returns>
public bool SaveSettings(string mySetValueName,string mySetValue)
{
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.CreateSubKey(myKey) ;
RegistryKey frjSubKey=frjKey.CreateSubKey(mySubKey) ;
try
{
frjSubKey.SetValue(mySetValueName,mySetValue) ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"保存键值错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return false;
}
frjSubKey.Close();
frjKey.Close();
return true;
}
/**//// <summary>
/// 读键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <returns></returns>
public string ReadSettings(string mySetValueName)
{
string ReadValueStr="";
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return ReadValueStr;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return ReadValueStr;
}
ReadValueStr=frjSubKey.GetValue(mySetValueName).ToString() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"读注册表错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
}
frjSubKey.Close();
frjKey.Close();
return ReadValueStr;
}
/**//// <summary>
/// 删除键值
/// </summary>
/// <param name="mySetValueName"></param>
/// <returns></returns>
public bool DeleteValue(string mySetValueName)
{
// RegistryKey MyReg = Registry.CurrentUser.OpenSubKey
// ("SOFTWARE\\SomeCompany\\SomeApp\\SomeVer", true);
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return false;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return false;
}
//frjSubKey.DeleteSubKeyTree(mySubKey) ; //递归删除子项和任何子级子项。字符串 subKey 不区分大小写。
// 备注
//必须具有适当权限才能删除子项及其目录树。
//警告 删除特定的项将移除目录树中该项下的所有项,并且不提供任何警告。
//如果想仅在子项没有子级子项时才删除该子项,请改用 DeleteSubKey。
frjSubKey.DeleteValue(mySetValueName,true);
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"删除键值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return false;
}
frjSubKey.Close();
frjKey.Close();
return true;
}
/**//// <summary>
/// 检索项中值的计数
/// </summary>
/// <returns></returns>
public int CountValue( )
{
int i=0;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return i;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return i;
}
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return 0;
}
i=frjSubKey.ValueCount;
frjSubKey.Close();
frjKey.Close();
return i;
}
/**//// <summary>
/// 检索与此项值关联的所有值名称的字符串数组
/// </summary>
/// <returns></returns>
public string[] GetSubKeyValueNames()
{
string[] ValueNames=null;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return ValueNames;
}
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
ValueNames=frjSubKey.GetValueNames() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
frjSubKey.Close();
frjKey.Close();
return ValueNames;
}
/**//// <summary>
/// To obtain a list of all the subkeys (child keys) that exist within your present RegistryKey object
/// </summary>
/// <returns></returns>
public string[] GetMySubKeyNames()
{
string[] mySubKeyNames=null;
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
RegistryKey frjKey=softwareKey.OpenSubKey(myKey) ;
if(frjKey==null)
{
frjKey.Close();
return mySubKeyNames;
}
mySubKeyNames=frjKey.GetSubKeyNames();
RegistryKey frjSubKey=frjKey.OpenSubKey (mySubKey) ;
try
{
if(frjSubKey==null)
{
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
//mySubKeyNames=frjSubKey.GetSubKeyNames() ;
}
catch(System.Exception e1)
{
MessageBox.Show(e1.ToString(),"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
frjSubKey.Close();
frjKey.Close();
return mySubKeyNames;
}
public bool AutoRunProgram()
{
string Pname=Application.ProductName;
string ProgramNameAndPath= Application.StartupPath+@"\"+Pname+".exe";
RegistryKey softwareKey=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
try
{
if(softwareKey==null)
{
//如果子键不存在则创建之
softwareKey=Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
string[] CheckPname=softwareKey.GetValueNames();
int i=CheckPname.Length;
// MessageBox.Show(i.ToString() ) ;
bool find=false;
int j=0;
while(j<i && !find)
{
if(CheckPname[j]==Pname)
{
find=true;
// MessageBox.Show(j.ToString() +" "+CheckPname[j] );
}
j++;
}
if(!find)
{
//在注册表中设置自动启动程序
softwareKey.SetValue(Pname, ProgramNameAndPath) ;
}
else
{
if(softwareKey.GetValue(Pname).ToString() !=ProgramNameAndPath )
softwareKey.SetValue(Pname, ProgramNameAndPath) ;
// else
// MessageBox.Show("已经有了") ;
}
}
catch
{
softwareKey.Close();
return false;
}
softwareKey.Close();
return true;
}
}
}