C#读写注册列表(写入注册列表,读取注册列表的数据)

69 篇文章 13 订阅

1.创建winfrom项目 ,在load事件中写方法(只看load事件里的代码就可以)

ps:如需要项目的源码,可去我的资源中下载

from1窗体代码

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test111111111111111
{
    public partial class Form1 : Form
    {
        RegistryKey key = Registry.LocalMachine;  获取到 当前登陆用户的 注册表启动项  必须声明为全局
        public static string KeyID = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        //读写注册列表
        RegistryKey software = key.OpenSubKey("software\\test12", true);
            //没有注册表信息
            if (software == null)    //判断如果没有
            {
                Form2 fm2 = new Form2();
                MessageBox.Show("机器尚未注册,请先注册");
                fm2.ShowDialog();//调取from2窗体
                software = key.CreateSubKey("software\\test12");//创建注册列表文件
                software.SetValue("KeyID", KeyID.ToString());//向注册列表文件中写入数据
            }
            //有注册表信息
            else
            {
                KeyID = software.GetValue("KeyID", "0").ToString();//如果不为null将注册列表的数据读出来
            }
            label1.Text = KeyID;//将读出的数据给label1
            //this.Width = 0;//让窗体消失
            //this.Height = 0;
            //this.Hide();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
           // if (label2.Text.Contains("11:35:00"))
            //{
              //  Thread.Sleep(1000);
              //  this.Width = 527;//让窗体显示
              //  this.Height = 357;
                //this.Show();
            //}
        }
    }
}

from2窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace test111111111111111
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private void Form2_Load_1(object sender, EventArgs e)
        {
            string constr = "Server =.; Database =test; User ID = sa ; Password = 123;";
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from demo1", con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "dept");
            部门
            comboBox1.DataSource = ds.Tables["dept"];
            comboBox1.DisplayMember = "dept";
            comboBox1.ValueMember = "dept";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //获取comboBox1选定的值
            string aa = comboBox1.SelectedValue.ToString();
            //MessageBox.Show(aa);
            if (aa != "" && aa != null)
            {
                Form1.KeyID = aa;//将从数据查到的数据给from1中的KeyID,然后写到注册列表
                this.Close();
            }
            else
            {
                MessageBox.Show("请选择部门信息");
                return;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香煎三文鱼

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值