我整合的读、写注册表的一段程序。可作参考。(C#编写,VB.net稍

来源:岁月联盟 编辑:zhu 时间:2003-07-12
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CProgramManage
{
    /// <summary>
    /// Summary description for FrmConfigure.
    /// </summary>
    public class FrmConfigure : System.Windows.Forms.Form
    {
        private Microsoft.Win32.RegistryKey parentKey;
        private bool bolExistKey;

        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtLServer;
        private System.Windows.Forms.TextBox txtLDataBase;
        private System.Windows.Forms.TextBox txtLLoginID;
        private System.Windows.Forms.CheckBox chkLNtLogin;
        private System.Windows.Forms.LinkLabel LinkCancel;
        private System.Windows.Forms.LinkLabel LinkOK;
        private System.Windows.Forms.LinkLabel LinkDefault;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.CheckBox chkRNtLogin;
        private System.Windows.Forms.TextBox txtRLoginID;
        private System.Windows.Forms.TextBox txtRDataBase;
        private System.Windows.Forms.TextBox txtRServer;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public FrmConfigure()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.chkLNtLogin = new System.Windows.Forms.CheckBox();
            this.txtLLoginID = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtLDataBase = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtLServer = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.chkRNtLogin = new System.Windows.Forms.CheckBox();
            this.txtRLoginID = new System.Windows.Forms.TextBox();
            this.txtRDataBase = new System.Windows.Forms.TextBox();
            this.txtRServer = new System.Windows.Forms.TextBox();
            this.LinkCancel = new System.Windows.Forms.LinkLabel();
            this.LinkOK = new System.Windows.Forms.LinkLabel();
            this.LinkDefault = new System.Windows.Forms.LinkLabel();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            //
            // tabControl1
            //
            this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                      this.tabPage1,
                                                                                      this.tabPage2});
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(256, 152);
            this.tabControl1.TabIndex = 0;
            //
            // tabPage1
            //
            this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.chkLNtLogin,
                                                                                   this.txtLLoginID,
                                                                                   this.label3,
                                                                                   this.txtLDataBase,
                                                                                   this.label2,
                                                                                   this.txtLServer,
                                                                                   this.label1});
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(248, 126);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Local Setup";
            //
            // chkLNtLogin
            //
            this.chkLNtLogin.Location = new System.Drawing.Point(72, 104);
            this.chkLNtLogin.Name = "chkLNtLogin";
            this.chkLNtLogin.Size = new System.Drawing.Size(72, 16);
            this.chkLNtLogin.TabIndex = 6;
            this.chkLNtLogin.Text = "Nt Login";
            this.chkLNtLogin.CheckedChanged += new System.EventHandler(this.chkLNtLogin_CheckedChanged);
            //
            // txtLLoginID
            //
            this.txtLLoginID.Location = new System.Drawing.Point(72, 72);
            this.txtLLoginID.Name = "txtLLoginID";
            this.txtLLoginID.Size = new System.Drawing.Size(160, 20);
            this.txtLLoginID.TabIndex = 5;
            this.txtLLoginID.Text = "";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(24, 76);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(46, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "LoginID:";
            //
            // txtLDataBase
            //
            this.txtLDataBase.Location = new System.Drawing.Point(72, 40);
            this.txtLDataBase.Name = "txtLDataBase";
            this.txtLDataBase.Size = new System.Drawing.Size(160, 20);
            this.txtLDataBase.TabIndex = 3;
            this.txtLDataBase.Text = "";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(16, 44);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(57, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "DataBase:";
            //
            // txtLServer
            //
            this.txtLServer.Location = new System.Drawing.Point(72, 8);
            this.txtLServer.Name = "txtLServer";
            this.txtLServer.Size = new System.Drawing.Size(160, 20);
            this.txtLServer.TabIndex = 1;
            this.txtLServer.Text = "";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(32, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Server:";
            //
            // tabPage2
            //
            this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.label4,
                                                                                   this.label5,
                                                                                   this.label6,
                                                                                   this.chkRNtLogin,
                                                                                   this.txtRLoginID,
                                                                                   this.txtRDataBase,
                                                                                   this.txtRServer});
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(248, 126);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Remote Setup";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(24, 75);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(46, 13);
            this.label4.TabIndex = 16;
            this.label4.Text = "LoginID:";
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(16, 43);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(57, 13);
            this.label5.TabIndex = 15;
            this.label5.Text = "DataBase:";
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(32, 11);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(41, 13);
            this.label6.TabIndex = 14;
            this.label6.Text = "Server:";
            //
            // chkRNtLogin
            //
            this.chkRNtLogin.Location = new System.Drawing.Point(74, 103);
            this.chkRNtLogin.Name = "chkRNtLogin";
            this.chkRNtLogin.Size = new System.Drawing.Size(72, 16);
            this.chkRNtLogin.TabIndex = 13;
            this.chkRNtLogin.Text = "Nt Login";
            this.chkRNtLogin.CheckedChanged += new System.EventHandler(this.chkRNtLogin_CheckedChanged);
            //
            // txtRLoginID
            //
            this.txtRLoginID.Location = new System.Drawing.Point(74, 71);
            this.txtRLoginID.Name = "txtRLoginID";
            this.txtRLoginID.Size = new System.Drawing.Size(160, 20);
            this.txtRLoginID.TabIndex = 12;
            this.txtRLoginID.Text = "";
            //
            // txtRDataBase
            //
            this.txtRDataBase.Location = new System.Drawing.Point(74, 39);
            this.txtRDataBase.Name = "txtRDataBase";
            this.txtRDataBase.Size = new System.Drawing.Size(160, 20);
            this.txtRDataBase.TabIndex = 10;
            this.txtRDataBase.Text = "";
            //
            // txtRServer
            //
            this.txtRServer.Location = new System.Drawing.Point(74, 7);
            this.txtRServer.Name = "txtRServer";
            this.txtRServer.Size = new System.Drawing.Size(160, 20);
            this.txtRServer.TabIndex = 8;
            this.txtRServer.Text = "";
            //
            // LinkCancel
            //
            this.LinkCancel.AutoSize = true;
            this.LinkCancel.Location = new System.Drawing.Point(208, 155);
            this.LinkCancel.Name = "LinkCancel";
            this.LinkCancel.Size = new System.Drawing.Size(39, 13);
            this.LinkCancel.TabIndex = 10;
            this.LinkCancel.TabStop = true;
            this.LinkCancel.Text = "Can&cel";
            this.LinkCancel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkCancel_LinkClicked);
            //
            // LinkOK
            //
            this.LinkOK.AutoSize = true;
            this.LinkOK.Location = new System.Drawing.Point(176, 155);
            this.LinkOK.Name = "LinkOK";
            this.LinkOK.Size = new System.Drawing.Size(21, 13);
            this.LinkOK.TabIndex = 9;
            this.LinkOK.TabStop = true;
            this.LinkOK.Text = "&OK";
            this.LinkOK.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkOK_LinkClicked);
            //
            // LinkDefault
            //
            this.LinkDefault.Location = new System.Drawing.Point(16, 155);
            this.LinkDefault.Name = "LinkDefault";
            this.LinkDefault.Size = new System.Drawing.Size(48, 13);
            this.LinkDefault.TabIndex = 11;
            this.LinkDefault.TabStop = true;
            this.LinkDefault.Text = "&Default";
            this.LinkDefault.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkDefault_LinkClicked);
            //
            // FrmConfigure
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(256, 173);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.LinkDefault,
                                                                          this.LinkCancel,
                                                                          this.LinkOK,
                                                                          this.tabControl1});
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmConfigure";
            this.Text = "Configure The Login Setup";
            this.Load += new System.EventHandler(this.FrmConfigure_Load);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void LinkDefault_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            txtLServer.Text="192.168.0.1";
            txtRServer.Text="1.1.1.1";
            txtLDataBase.Text="Data";
            txtRDataBase.Text="Data";
        }

        private void chkRNtLogin_CheckedChanged(object sender, System.EventArgs e)
        {
            if(this.chkRNtLogin.Checked)
            {
                txtRLoginID.Enabled=false;
            }
            else
            {
                txtRLoginID.Enabled=true;
            }
        }

        private void FrmConfigure_Load(object sender, System.EventArgs e)
        {
            //txtLServer.Text=System.GetSetting("ProgramManage","LogonInfo","LocalServer","192.168.0.1");
            //string[] reabc;
            try
            {
                //reabc=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValueNames();
                txtLServer.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("LocalServer").ToString();
                txtLDataBase.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("LocalDataBase").ToString();
                txtLLoginID.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("LocalLoginID").ToString();
                if(Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("LocalNtLogin").ToString()=="0")
                {
                    chkLNtLogin.Checked=false;
                }
                else
                {
                    chkLNtLogin.Checked=true;
                }
                txtRServer.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("RemoteServer").ToString();
                txtRDataBase.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("RemoteDataBase").ToString();
                txtRLoginID.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("RemoteLoginID").ToString();
                if(Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue("RemoteNtLogin").ToString()=="0")
                {
                    chkRNtLogin.Checked=false;
                }
                else
                {
                    chkRNtLogin.Checked=true;
                }
            }
            catch (Exception k)
            {
                MessageBox.Show(k.Message,k.Source+k.GetHashCode(),MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
/*
            for (int i=0;i<reabc.Length;i++)
            {
                if(reabc[i]!="LocalServer")
                {
                    txtLServer.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue(reabc[i]).ToString();
                }
                else
                {
                    txtRServer.Text=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare").OpenSubKey("ProgramManage").GetValue(reabc[i]).ToString();
                }
            }
*/            //txtLServer.Text=reabc[0];
            //txtLDataBase.Text=reabc[1];
            //txtLLoginID.Text=reabc[2];
        }

        private void chkLNtLogin_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkLNtLogin.Checked)
            {
                txtLLoginID.Enabled=false;
            }
            else
            {
                txtLLoginID.Enabled=true;
            }
        }

        private void LinkOK_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            
            try
            {
                parentKey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare",true).OpenSubKey("ProgramManage",true);
            }
            catch (Exception l)
            {
                bolExistKey=true;
            }

            if (bolExistKey)
            {
                parentKey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SoftWare",true).CreateSubKey("ProgramManage");
            }

                //parentKey.
                parentKey.SetValue("LocalServer",txtLServer.Text);
                parentKey.SetValue("LocalDataBase",txtLDataBase.Text);
                parentKey.SetValue("LocalLoginID",txtLLoginID.Text);
                if (chkLNtLogin.Checked )
                {
                    parentKey.SetValue("LocalNtLogin","1");
                }
                else
                {
                    parentKey.SetValue("LocalNtLogin","0");
                }
                parentKey.SetValue("RemoteServer",txtRServer.Text);
                parentKey.SetValue("RemoteDataBase",txtRDataBase.Text);
                parentKey.SetValue("RemoteLoginID",txtRLoginID.Text);
                if (chkRNtLogin.Checked )
                {
                    parentKey.SetValue("RemoteNtLogin","1");
                }
                else
                {
                    parentKey.SetValue("RemoteNtLogin","0");
                }
            this.Close();
        }

        private void LinkCancel_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }
    }
}