一、问题起因及来由
最近由于自己工作需要,把丢了十几年没有用过的编程软件知识捡起来,虽然十几年了,但还是忘不了,还是喜欢自己敲代码,遇到问题后,体验解决问题的后的快感。
同时也喜欢看着自己敲出的一系列整齐的代码,看着真实赏心悦目。
所以呢,为了工作需要,打算做一个内部使用小软件。白天工作时间需要处理日常工作问题(非计算机编程业务),所以只能下班后,检查完孩子作业后,奋战几个小时。
二、问题类型
这个小软件主要是关于客户管理、内部员工业务、财务等等相关。其中不乏需要针对每个用户有不同的权限及处理不同的业务内容。所以绕不开的就是登录权限问题。都知道登录都好解决,无非就是增删改查。但是一直使用计算机软件,都知道登录有几个功能比较好用,比如记住密码和自动登录,当然还有一个比较心情好可用,心情不好看着烦的开机启动功能。
三、具体问题
对,没有错,就是在保存密码和自动登录,这两个功能实现的遇到的,需要实现这两个功能还是相对容易,从思维角度看,无外乎就是读写文件问题。为了方便我选择了把账号密码存入App.config这个XML类型的配置文件里面。当然数据库连接也是存里面的。具体代码块如下:
` //用户登录
private void userlogin()
{
if (txtName.Text.Trim().Equals("") || txtpw.Text.Trim().Equals(""))
{
MessageBox.Show("姓名密码不能为空!");
return;
}
else
{
BLL.tbUser userBLL = new BLL.tbUser();
if (userBLL.select("Uname='" + txtName.Text.Trim() + "' and Upassword='" + txtpw.Text.Trim() + "'").Rows.Count > 0)
{
MessageBox.Show("登录成功!");
Adduser au = new Adduser();
au.Tag = this;
au.Show();
this.Hide();
Saveuser(txtName.Text.Trim(), txtpw.Text.Trim());
}
else {
MessageBox.Show("密码或姓名错误!");
return;
}
}
}`
` //自动登录
private bool IsAutoLoginEnable()
{
try {
Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (cf.AppSettings.Settings["autoLogin"].Value == "true" && cf.AppSettings.Settings["rememberme"].Value == "true")
{
txtName.Text = cf.AppSettings.Settings["username"].Value;
txtpw.Text = cf.AppSettings.Settings["userpw"].Value;
BLL.tbUser userBLL = new BLL.tbUser();
if (userBLL.select("Uname='" + cf.AppSettings.Settings["username"].Value + "' and Upassword='" + cf.AppSettings.Settings["userpw"].Value + "'").Rows.Count > 0)
{
Adduser au = new Adduser();
au.Tag = this;
au.Show();
this.Hide();
}
return true;}else{return false;}}catch {return false;}}`
` //接收配置对象、键名和值
private void SetConfigValue(Configuration cf, string key, string value)
{
if (cf.AppSettings.Settings[key] != null)//如果键存在
{
cf.AppSettings.Settings[key].Value = value;//更新值
}
else//如果不存在
{
cf.AppSettings.Settings.Add(key, value);//添加新值
}
}
//保存用户名和密码
private void Saveuser(string username, string userpw)
{
try
{
//打开配置文件
Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (checklogin.Checked)//如果选择自动登录
{
SetConfigValue(cf, "rememberme", "true");//设置记住密码为true
SetConfigValue(cf, "username", username);
SetConfigValue(cf, "userpw", userpw);
SetConfigValue(cf, "autoLogin", "true");
}else if (checkpw.Checked){SetConfigValue(cf, "rememberme", "true");//设置记住密码为trueSetConfigValue(cf, "username", username);SetConfigValue(cf, "userpw", userpw);SetConfigValue(cf, "autoLogin", "false");}else{SetConfigValue(cf, "rememberme", "false");//设置记住密码为trueSetConfigValue(cf, "username", "");SetConfigValue(cf, "userpw", "");SetConfigValue(cf, "autoLogin", "false");}cf.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("appSettings");}catch(Exception ex) {MessageBox.Show("保存密码时出错"+ex.Message);}}`
每次运行调试都是正常,但是,对,但是第二次运行的又还原了,就跟系统还原一样。一直以为是代码问题。
最后也网上各种查找,看到有人也遇到过。
四、解决方案
其实主要的问题是,这压根就不是一个问题,为什么,因为我们每次都是运行调试,所以每次这个文件都会恢复默认状态,所以从程序运行插入的动态数据,保存了也会恢复原来的状态。除非是把程序重新解决方案,同时在bin文件夹找到程序的.exe文件,进行运行,这个时候你会发现,功能完全是没有任何问题。因为VS的F5就是一个测试编译器,所以每次都会恢复默认状态,不会动态存储内容。
现在马上十二点【2025年10月23日23:38】,太晚了睡觉去了。遇到其他问题了再分享。
感谢大家!!