C#一个可调用的密码验证和修改的弹窗,用一个文件实现复用

//C#实现密码验证弹窗和密码修改弹窗,用一个文件实现复用

//密码弹窗代码(需要手动创建一个.cs窗口代码)
// 自定义密码对话框窗体
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace PCSTools//声明为主窗口的域
{
    public partial class PasswordDialogForm : Form
    {
        private Label lblPrompt;
        private TextBox oldtxtPassword;
        private Label lblPrompt1;
        private TextBox newtxtPassword;

        private Button btnOk;
        private Button btnCancel;
        string PasswordfilePathName = System.Environment.CurrentDirectory + "\\ConfigData\\password.dat";
        public string Password { get; private set; }

        public PasswordDialogForm(string Prompt, string title,int num)
        {
            InitializeComponents(Prompt, title,num);
        }

        private void InitializeComponents(string Prompt, string title, int num)
        {
            this.Text = title;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.AcceptButton = btnOk;
            this.CancelButton = btnCancel;

            if (1 == num)
            {
                lblPrompt = new Label();
                lblPrompt.Text = Prompt;
                lblPrompt.SetBounds(12, 20, 372, 13);

                oldtxtPassword = new TextBox();
                oldtxtPassword.PasswordChar = '*';
                oldtxtPassword.SetBounds(12, 36, 372, 20);

                btnOk = new Button();
                btnOk.Text = "确定";
                btnOk.DialogResult = DialogResult.OK;
                btnOk.SetBounds(228, 72, 75, 23);

                btnCancel = new Button();
                btnCancel.Text = "取消";
                btnCancel.DialogResult = DialogResult.Cancel;
                btnCancel.SetBounds(309, 72, 75, 23);
                this.ClientSize = new System.Drawing.Size(396, 107);
                this.Controls.AddRange(new Control[] { lblPrompt, oldtxtPassword, btnOk, btnCancel });

                btnOk.Click += (sender, e) =>
                {
                    Password = oldtxtPassword.Text;
                };
            }
            if(2 == num)
            {
                lblPrompt = new Label();
                lblPrompt.Text = "输入旧密码";
                lblPrompt.SetBounds(12, 17, 372, 13);

                oldtxtPassword = new TextBox();
                oldtxtPassword.PasswordChar = '*';
                oldtxtPassword.SetBounds(12, 33, 372, 20);

                lblPrompt1 = new Label();
                lblPrompt1.Text = "输入新密码";
                lblPrompt1.SetBounds(12, 20 + 42, 372, 13);

                newtxtPassword = new TextBox();
                newtxtPassword.PasswordChar = '*';
                newtxtPassword.SetBounds(12, 36 + 42, 372, 20);

                btnOk = new Button();
                btnOk.Text = "确定";
                btnOk.DialogResult = DialogResult.OK;
                btnOk.SetBounds(228, 72 + 42, 75, 23);

                btnCancel = new Button();
                btnCancel.Text = "取消";
                btnCancel.DialogResult = DialogResult.Cancel;
                btnCancel.SetBounds(309, 72 + 42, 75, 23);

                this.ClientSize = new System.Drawing.Size(396, 107 + 42);
                this.Controls.AddRange(new Control[] { lblPrompt, oldtxtPassword, lblPrompt1, newtxtPassword, btnOk, btnCancel });
                btnOk.Click += (sender, e) =>
                {
                    if (File.ReadAllText(PasswordfilePathName) != oldtxtPassword.Text)
                    {
                        MessageBox.Show("原密码错误,请重试!");
                        return;
                    }
                    if (newtxtPassword.Text == "")
                    {
                        MessageBox.Show("请输入新密码!");
                        return;
                    }
                    if (newtxtPassword.TextLength > 6)
                    {
                        MessageBox.Show("新密码设置超过6位!");
                        return;
                    }

                    File.SetAttributes(PasswordfilePathName, FileAttributes.Normal);                    
                    //写入新的密码
                    using (StreamWriter writer = new StreamWriter(PasswordfilePathName))
                    {
                        writer.Write(newtxtPassword.Text);
                    }
                    //密码文件修改只读不可见
                    File.SetAttributes(PasswordfilePathName, FileAttributes.ReadOnly | FileAttributes.Hidden);
                    MessageBox.Show("新密码设置成功!");
                    this.Close();
                };
            }

            btnCancel.Click += (sender, e) =>
            {
                this.Close();
            };
        }
    }
}

//密码文件初始化位(放在主窗口初始化的地方,打开exe文件时就要执行完毕)

string password_init = "admin";
if (!File.Exists(PasswordfilePathName))
 {
     // 创建文件并写入字符串
     File.WriteAllText(PasswordfilePathName, password_init);
     //密码文件修改只读不可见
     File.SetAttributes(PasswordfilePathName, FileAttributes.ReadOnly | FileAttributes.Hidden);
 }

1、需要密码验证的代码调用处


//下面的代码放在需要密码验证的地方(比如按钮操作内部)
string input = ShowPasswordDialog("请输入密码:", "密码验证",1);

if (input == File.ReadAllText(PasswordfilePathName))
{
    //MessageBox.Show("密码正确,正在打开操作界面...");
    ConnectModbusSlave();
}
else 
{
    MessageBox.Show("密码错误!请重试!");
}
// 自定义密码对话框
private string ShowPasswordDialog(string Prompt, string title, int num)
{
    PasswordDialogForm dialogForm = new PasswordDialogForm(Prompt, title, num);
    DialogResult dialogResult = dialogForm.ShowDialog(this);

    if (dialogResult == DialogResult.OK)
    {
        return dialogForm.Password;
    }
    else
    {
        return "";
    }
}

在这里插入图片描述

2、修改密码的代码调用处

public PasswordDialogForm passwordForm;//申明密码窗口
passwordForm = new PasswordDialogForm("输入原密码","修改密码",2);
passwordForm.Show();

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值