用XML记住账号密码程序如下:
string username;
string pwd;
string path = @"XMLFile1.xml";
private void check_CheckedChanged(object sender, EventArgs e)
{
username = textBox1.Text;
pwd = textBox2.Text;
saveToXML(username, pwd);
}
private void saveToXML(string username, string pwd)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);
XmlNode node;
node = xmldoc.SelectSingleNode("XMLFile1/username");
if (node == null)
{
XmlElement n = xmldoc.CreateElement("username");
n.InnerText = username;
xmldoc.SelectSingleNode("XMLFile1").AppendChild(n);
}
else
{
node.InnerText = username;
}
node = xmldoc.SelectSingleNode("XMLFile1/pwd");
if (node == null)
{
XmlElement n = xmldoc.CreateElement("pwd");
n.InnerText = pwd;
xmldoc.SelectSingleNode("XMLFile1").AppendChild(n);
}
else
{
node.InnerText = pwd;
}
xmldoc.Save(path);
}
private void getFromXml()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);
XmlNode node;
node = xmldoc.SelectSingleNode("XMLFile1/username");
username = node.InnerText;
node = xmldoc.SelectSingleNode("XMLFile1/pwd");
pwd = node.InnerText;
textBox1.Text = username;
textBox2.Text = pwd;
}
运行后选中复选框退出后在运行依旧没有记住密码