//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();