本地、文件实现记住密码功能
刚学C#,初步了解了下Windows窗体应用,虽然这个问题对前辈们很简单,但还是请求帮助:编写了一个简单的聊天应用,但是不知道怎么实现记住密码功能,百度了很多,但都是用不了,希望有前辈们能指教,有源代码更好
感谢!
程序代码: string username = comboBox1.SelectedItem.ToString();//用于检索
XElement rootNode = XElement.Load("User.xml");//加载xml文件
var query = from serch in rootNode.Elements("user") where serch.Descendants("id").Select(sel => sel.Value).Contains("" + username + "") select serch;
foreach (XElement node in query)
{
serstr = node.Element("id").Value.ToString();//把查到的id值放到Serstr变量中
}
//登陆界面checkbox控件,打上勾就是保存密码
if (checkBox1.Checked == true&&serstr!=comboBox1.Text)//如果xml没有这个用户并且打上勾了,就保存到xml文件中
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("User.xml");
XmlNode root = xmldoc.SelectSingleNode("UserLogin");//查找根节点
XmlElement xesub1 = xmldoc.CreateElement("user");//获得子节点
XmlElement useid = xmldoc.CreateElement("id");//用户名
useid.InnerText = comboBox1.Text.Trim();//用户名添加到xml文件id元素中
xesub1.AppendChild(useid);//添加到user节点中
XmlElement usepwd = xmldoc.CreateElement("pwd");//创建密码pwd属性
usepwd.InnerText = textBox1.Text.Trim();//添加密码到xml文件pwd中
xesub1.AppendChild(usepwd);//pwd添加到user节点中
root.AppendChild(xesub1);//user节点添加到根节点userlogin中
xmldoc.Save("User.xml"); //保存xml文件
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//==================
this.DialogResult = DialogResult.OK;
this.Close();
