Registry类 获取注册表信息

资料:https://msdn.microsoft.com/zh-cn/library/microsoft.win32.registry.aspx

 

Registry需要引用using Microsoft.Win32;

public void getStr()
{
  XmlDocument xd = new XmlDocument();
  string strpath = Application.StartupPath + "\\配置文件\\Profile.xml";
  xd.Load(strpath);
  XmlNode xn = xd.DocumentElement;
  string value = xn.ChildNodes[0]["LinkBit"].InnerText;

  string server="";
  string dataname = "";
  string pwd="";
  string userid="";

  RegistryKey rk = Registry.CurrentConfig.OpenSubKey("ICareWorkAirConditioningFT");//读取注册表
  if (rk != null)
  {
    server = rk.GetValue("服务器名称").ToString();
    dataname = rk.GetValue("数据库名称").ToString();
    userid = rk.GetValue("用户名", "Null").ToString();
    pwd = rk.GetValue("密码", "Null").ToString();
  }
  if (value.Equals("False"))
  {
    txtservername.Text = server;
    txtdataname.Text = dataname;
    txtPWD.Text = pwd;
    txtOp.Text = userid;
    IsSave = false;
    if (DatabaseLinkMode == 1) LinkDatabase();
  }
}

 

-----------Profile.xml文件

<?xml version="1.0" encoding="utf-8"?>
<ReadWay>
<LinkDatabase>
<!--
数据库连接标志,值为TRUE,要弹出数据库连接界面,否则直接调用注册表中的值连接。
-->
<LinkBit>False</LinkBit>
</LinkDatabase>
<ReportPath>
<Path>D:\</Path>
</ReportPath>
<WaySelect>
<way>hx</way>
<!--
GPRS:GPRS抄表方式
RS485:RS232抄表方式
-->
</WaySelect>
<RS485>
<com>AA</com>
<btl>BB</btl>
<xyfs>CC</xyfs>
<!--
AA:串口号。如:COM1等。
BB:波特率。如:1200等。
CC:校验方式。0:无校验 1:奇校验 2:偶校验
-->
</RS485>
<GPRS>
<port>PP</port>
<!--
pp:连接GPRS模块使用的端口号。如:3000等。
-->
</GPRS>
<SD>
<KZZ>04</KZZ>
<BSF>C03A</BSF>
<!--
KZZ:送电所用的控制字。
BSF:送电所用的标识符。
-->
</SD>
<GD>
<KZZ>04</KZZ>
<BSF>C03B</BSF>
<!--
KZZ:关电所用的控制字。
BSF:关电所用的标识符。
-->
</GD>
<PurchCard>
<PurchCardID>COM2</PurchCardID>
<!--
红外写卡机通讯地址。如:000000000001等。
-->
</PurchCard>
<Comm>
<CommNum>COM1</CommNum>
<!--
COM端口号。如:COM1等。
-->
</Comm>
<Url>
<url>http://10.168.1.226:8816/Action</url>
<!--
中间件连接地址。如:http://10.168.1.226:8816/Action等。
-->
</Url>
<Partner>
<partner>
</partner>
<!--
中间件合作者账号。如:等。
-->
</Partner>
<Key>
<key>
</key>
<!--
中间件64位秘钥。如:等。
-->
</Key>
<Timeout>
<time>20</time>
<!--
中间件调用超时时长。如:10秒等。
-->
</Timeout>
</ReadWay>

 

 

 

补充:

C#读取注册表非常简单方便,指定路径后,设置对应的字段和属性值即可。

但是从Windows Vista开始,读取注册表就开始需要权限了。当然可以简单的使用管理员权限打开,或者在程序的安全性中设置其打开方式为管理员都行。但是这样多少还是在客户端后的用户层面的设置。如何才能把些工作放在开放端呢。
经过研究发现有2种办法。
方法一
1、使用ClickOnce设置,这个方法网上都有就是在项目属性的安全性中选择Click Once安全选项。
2、再app.manifast中进行修改,可以在其中看到以下文字
 
       <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC 清单选项
            如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换 
            requestedExecutionLevel 节点。
 
        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" /> 
        
            指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
            如果要利用文件和注册表虚拟化实现向后 
            兼容性,则删除 requestedExecutionLevel 节点。
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
……
  </security>
 
注册的部分已经说的非常清楚,根据需要进行修改即可。但是无论选择其他任选的2个,在编译时都会报错。其实这里再把Click Once的安全选项关闭即可,因为app.manifest文件已经存在了。
 
方法二
在C#的RegistryKey中就包括了安全选项的内容。
 
        private static void SetRegister()
        {
            string regpath = "SOFTWARE\\Q-Das\\QTrans\\UAES";
            RegistryKey uaes = Registry.LocalMachine.OpenSubKey(regpath,
                RegistryKeyPermissionCheck.ReadWriteSubTree,System.Security.AccessControl.RegistryRights.FullControl); 
            if (uaes == null)
                uaes = Registry.LocalMachine.CreateSubKey(regpath, RegistryKeyPermissionCheck.ReadWriteSubTree); 
            uaes.SetValue("Version", "1.0", RegistryValueKind.String);
            uaes.SetValue("Author", "Andrew Hao", RegistryValueKind.String);
            uaes.SetValue("LastUpdate", "2013-2-22", RegistryValueKind.String);
            uaes.SetValue("AppPath", Application.StartupPath, RegistryValueKind.String);
 
            object obj = uaes.GetValue("InstallDate", null);
            if (obj == null)
                uaes.SetValue("InstallDate", DateTime.Now.ToString(), RegistryValueKind.String);
 
            obj = uaes.GetValue("InstallDate", "Failed...");
            MessageBox.Show(obj.ToString());
        }
上面加黑部分指定了相应的权限,选择FullControl即可获得全部权限。

 

posted @ 2017-05-23 11:02  我叫阿良❤善良的良  阅读(344)  评论(0)    收藏  举报