如何在c#中创建注册表 [英] how to create registry in c#

查看:368
本文介绍了如何在c#中创建注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生我想制作注册表,这是我的代码..

hello sir i want to make registry , here is my code..

RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\NERP");
if (key == null)
{
    locServername  = txt_locServer .Text; //.
    locDatabaseName  = txt_locDb .Text;//employee
    locUsername  = txt_locName .Text;//sa
    locPwd  = txt_locPwd .Text;//.
    MainServername = txt_ServerName.Text;
    MainUsername = txt_ServerLoginName.Text ;
    MainPwd = txt_Severpwd.Text;
    
    
    key.SetValue("LocServer", locServername);
    key.SetValue("LocDB", locDatabaseName);
    key.SetValue("LocUsername", locUsername);
    key.SetValue("LocPassword", locPwd);
    key.SetValue("Mainserver", MainServername);
    key.SetValue("MainUsername", MainUsername);
    key.SetValue("MainPwd", MainPwd);
    
    MessageBox.Show("Registry created");
    this.Close();





当我跑这个程序有错误像

对象引用没有设置为对象的实例在这一行

key.SetValue(LocServer,locServername);



在locServername中我传递值。 ;

先生告诉我该怎么做。

i想让连接字符串看起来像这样

datasource =。; database = employee; user id = sa; password ='';



when i run this program there is error like
object reference not set to an instance of an object" at this line
key.SetValue("LocServer", locServername);

in the "locServername" I pass value ".";
sir tell me how to do this.
i want to make connection string look like this
datasource=.;database=employee;user id=sa;password='';

推荐答案

声明

The statement
if (key == null)


代码中的
,意味着如果您对 OpenSubKey 的调用失败,您将只执行以下代码。因此您可以保证得到该错误。它应该是


in your code, means that you will only execute the following code if your call to OpenSubKey fails. So you are guaranteed to get that error. It should be

if (key != null)



您还应该调查为什么您对 OpenSubKey 的调用失败。


您的错误是按设计:以下条件

Your error is by design: the following condition
Quote:

if(key == null)

if (key == null)

保证

Quote

key.SetValue( key 为null到达LocServer,locServername);

key.SetValue("LocServer", locServername);









你错过了一步:创建 如果它尚不存在。


这篇关于如何在c#中创建注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆