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#读取注册表非常简单方便,指定路径后,设置对应的字段和属性值即可。
|
<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>
|
| 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());
}
|

浙公网安备 33010602011771号