C#操作注册表的两种方法及引申出调用脚本的通用方法

今天着急改天补详细用法

1)using Microsoft.Win32;这是常规的方法,有很多详细介绍,这不是重点

ContractedBlock.gif ExpandedBlockStart.gif Code
        public static string GetRegValue(string strRegpath, string strRegKeyName)
        {
            
string str;
            
try
            {
                RegistryKey rk 
= Registry.LocalMachine.OpenSubKey(strRegpath, true);

                
if (rk != null)
                {
                    str 
= rk.GetValue(strRegKeyName).ToString();
                }
                
else
                {
                    str 
= "Unable find the specified registry key or value";
                }

            }
            
catch (Exception e)
            {
                str 
= "Unable find the specified registry key or value,Vista May need close UAC ! " + e;
            }
            
return str;
        }

 

2)引用wshom.ocx:这提供了一条途径复用各种脚本,当framework提供的方法不好用时可以考虑。暂时只是觉得有用,还不知道怎么用,呵呵,atc那些乱七八糟的脚本都可以用了

var Reg = new ActiveXObject("WScript.Shell");//找到WScript.Shell所在的ocx引用之

Reg.read();

ContractedBlock.gif ExpandedBlockStart.gif Code
        public static string GetRegValue(string strRegPath)
        {
            
string strRegValue;
            
try
            {
                IWshRuntimeLibrary.WshShell ws 
= new IWshRuntimeLibrary.WshShell();
                strRegValue 
= ws.RegRead(strRegPath).ToString();
            }
            
catch (Exception e)
            {
                strRegValue 
= "Unable find the specified registry key or value,Vista May need close UAC ! " + e;
            }
            
return strRegValue;
        }

 

 

转载于:https://www.cnblogs.com/hbreset/archive/2008/08/06/1262240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值