读写注册表

None.gif 使用
None.gifRegistryManager rwRgs
= new  RegistryManager() ;
None.gif        
private   void  button4_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string str1=rwRgs.ReadSettings("myName") ;
InBlock.gif            
if(str1!="")
InBlock.gif                MessageBox.Show(str1) ;
InBlock.gif        
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif        
private   void  button5_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(rwRgs.SaveSettings("myName","furenjun"+System.DateTime.Now.ToString()))
InBlock.gif                MessageBox.Show(
"save Success") ;
InBlock.gif
InBlock.gif  
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  button6_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(rwRgs.DeleteValue("myName"))
InBlock.gif                MessageBox.Show(
"del Success") ;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  button7_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif             
if(rwRgs.CountValue() >0)
InBlock.gif                 MessageBox.Show (rwRgs.CountValue().ToString() );
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  button8_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
string[] str1=rwRgs.GetSubKeyValueNames();
InBlock.gif            str1
=rwRgs.GetMySubKeyNames();  
InBlock.gif            
if(str1!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
int i=str1.Length;
InBlock.gif                MessageBox.Show(i.ToString() ) ;
InBlock.gif                
string s1="";
InBlock.gif                
for(int j=0;j<i;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    s1
+=str1[j]+" ";
ExpandedSubBlockEnd.gif                }

InBlock.gif                MessageBox.Show(s1.ToString() ) ;
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  button9_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            MessageBox.Show ( Application.ProductName);
ExpandedBlockEnd.gif        }
None.gif using  System;
None.gif
using  System.Windows.Forms;
None.gif
using  Microsoft.Win32;
None.gif
namespace  ReadWriteRegiest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// RegistryManager 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class RegistryManager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public RegistryManager()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
string myKeyStr="frj";
InBlock.gif        
string mySubKeyStr="frjChild";
InBlock.gif        
public string myKey
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return myKeyStr;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{myKeyStr=value;}
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string mySubKey
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return mySubKeyStr;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{mySubKeyStr=value;}
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 保存键值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mySetValueName"></param>
InBlock.gif        
/// <param name="mySetValue"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public  bool SaveSettings(string mySetValueName,string mySetValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            RegistryKey frjKey
=softwareKey.CreateSubKey(myKey) ;
InBlock.gif            RegistryKey frjSubKey
=frjKey.CreateSubKey(mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                frjSubKey.SetValue(mySetValueName,mySetValue) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"保存键值错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif
InBlock.gif                frjSubKey.Close();
InBlock.gif                frjKey.Close();
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 读键值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mySetValueName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string  ReadSettings(string mySetValueName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string ReadValueStr="";
InBlock.gif            
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            RegistryKey frjKey
=softwareKey.OpenSubKey(myKey) ;
InBlock.gif            
if(frjKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                frjKey.Close();
InBlock.gif                
return ReadValueStr;
ExpandedSubBlockEnd.gif            }

InBlock.gif            RegistryKey frjSubKey
=frjKey.OpenSubKey (mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
if(frjSubKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    frjSubKey.Close();
InBlock.gif                    frjKey.Close();
InBlock.gif                    
return ReadValueStr;
ExpandedSubBlockEnd.gif                }

InBlock.gif                ReadValueStr
=frjSubKey.GetValue(mySetValueName).ToString()  ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"读注册表错误",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif            
return ReadValueStr;
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除键值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mySetValueName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool DeleteValue(string mySetValueName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
InBlock.gif            
//                RegistryKey MyReg = Registry.CurrentUser.OpenSubKey
InBlock.gif            
//                ("SOFTWARE\\SomeCompany\\SomeApp\\SomeVer", true);
InBlock.gif

InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            
InBlock.gif            RegistryKey frjKey
=softwareKey.OpenSubKey(myKey) ;
InBlock.gif            
if(frjKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                frjKey.Close();
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            RegistryKey frjSubKey
=frjKey.OpenSubKey (mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
if(frjSubKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    frjSubKey.Close();
InBlock.gif                    frjKey.Close();
InBlock.gif                    
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//frjSubKey.DeleteSubKeyTree(mySubKey) ; //递归删除子项和任何子级子项。字符串 subKey 不区分大小写。
InBlock.gif                
//                备注
InBlock.gif                
//必须具有适当权限才能删除子项及其目录树。
InBlock.gif                
//警告   删除特定的项将移除目录树中该项下的所有项,并且不提供任何警告。
InBlock.gif                
//如果想仅在子项没有子级子项时才删除该子项,请改用 DeleteSubKey。
InBlock.gif

InBlock.gif                frjSubKey.DeleteValue(mySetValueName,
true);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"删除键值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif                
InBlock.gif                frjSubKey.Close();
InBlock.gif                frjKey.Close();
InBlock.gif                
return false;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif            
return true;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检索项中值的计数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int  CountValue( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i=0;
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            RegistryKey frjKey
=softwareKey.OpenSubKey(myKey) ;
InBlock.gif            
if(frjKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                frjKey.Close(); 
InBlock.gif                
return i;
ExpandedSubBlockEnd.gif            }

InBlock.gif                
InBlock.gif            RegistryKey frjSubKey
=frjKey.OpenSubKey (mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
if(frjSubKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    frjSubKey.Close();
InBlock.gif                    frjKey.Close();
InBlock.gif                    
return i;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif                frjSubKey.Close();
InBlock.gif                frjKey.Close();
InBlock.gif                
return 0;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            i
=frjSubKey.ValueCount;  
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif
InBlock.gif            
return i;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif         
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检索与此项值关联的所有值名称的字符串数组
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string[] GetSubKeyValueNames()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] ValueNames=null
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            RegistryKey frjKey
=softwareKey.OpenSubKey(myKey) ;
InBlock.gif            
if(frjKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                frjKey.Close(); 
InBlock.gif                
return ValueNames;
ExpandedSubBlockEnd.gif            }

InBlock.gif                
InBlock.gif            RegistryKey frjSubKey
=frjKey.OpenSubKey (mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
if(frjSubKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    frjSubKey.Close();
InBlock.gif                    frjKey.Close();
InBlock.gif                    
return ValueNames;
ExpandedSubBlockEnd.gif                }

InBlock.gif                ValueNames
=frjSubKey.GetValueNames() ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif                frjSubKey.Close();
InBlock.gif                frjKey.Close();
InBlock.gif                
return ValueNames;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif              
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif
InBlock.gif            
return ValueNames;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// To obtain a list of all the subkeys (child keys) that exist within your present RegistryKey object
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string[] GetMySubKeyNames()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] mySubKeyNames=null
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows",true);
InBlock.gif            RegistryKey frjKey
=softwareKey.OpenSubKey(myKey) ;
InBlock.gif            
if(frjKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                frjKey.Close(); 
InBlock.gif                
return mySubKeyNames;
ExpandedSubBlockEnd.gif            }

InBlock.gif            mySubKeyNames
=frjKey.GetSubKeyNames();  
InBlock.gif
InBlock.gif            RegistryKey frjSubKey
=frjKey.OpenSubKey (mySubKey) ;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
if(frjSubKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    frjSubKey.Close();
InBlock.gif                    frjKey.Close();
InBlock.gif                    
return mySubKeyNames;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//mySubKeyNames=frjSubKey.GetSubKeyNames()  ;
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
catch(System.Exception e1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(e1.ToString(),
"统计检索项中值出错",MessageBoxButtons.OK ,MessageBoxIcon.Information );
InBlock.gif                frjSubKey.Close();
InBlock.gif                frjKey.Close();
InBlock.gif                
return mySubKeyNames;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif              
InBlock.gif            frjSubKey.Close();
InBlock.gif            frjKey.Close();
InBlock.gif
InBlock.gif            
return mySubKeyNames;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool AutoRunProgram()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
string Pname=Application.ProductName;
InBlock.gif            
string ProgramNameAndPath= Application.StartupPath+@"\"+Pname+".exe";
InBlock.gif            RegistryKey softwareKey
=Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(softwareKey==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//如果子键不存在则创建之
InBlock.gif
                    softwareKey=Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
string[] CheckPname=softwareKey.GetValueNames(); 
InBlock.gif                
int i=CheckPname.Length;
InBlock.gif
//                MessageBox.Show(i.ToString() ) ;
InBlock.gif
                bool find=false;
InBlock.gif                
int j=0;
InBlock.gif                
while(j<&& !find)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(CheckPname[j]==Pname)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{    
InBlock.gif                        find
=true;
InBlock.gif
//                        MessageBox.Show(j.ToString() +"   "+CheckPname[j] );
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    j
++;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                 
InBlock.gif                
if(!find)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//在注册表中设置自动启动程序
InBlock.gif
                    softwareKey.SetValue(Pname, ProgramNameAndPath) ;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(softwareKey.GetValue(Pname).ToString() !=ProgramNameAndPath )
InBlock.gif                        softwareKey.SetValue(Pname, ProgramNameAndPath) ;
InBlock.gif
//                    else
InBlock.gif
//                        MessageBox.Show("已经有了") ;
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                softwareKey.Close();  
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            softwareKey.Close(); 
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
posted on 2006-04-28 19:05 DotNet编程 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/furenjun/archive/2006/04/28/387806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值