这仅用于演示目的。我已经收到有关应用程序设置不安全的建议,实际上我不会以这种方式缓存密码。
我正在创建一个登录Windows Forms应用程序,该应用程序在使用Application Settings的应用程序会话之间保留在Name
和Password
字段中输入的字符串值。表单应加载从上一个会话输入的Name
和Password
值。
我遇到一个问题,如果我使用Properties.Settings.Default.Save()从私有方法保存这些字段值,则无法保存和重新加载这些字段:
// Save the current values of each field
private void SaveSettings()
{
Properties.Settings.Default.Name = textBox1.Text;
Properties.Settings.Default.Password = textBox2.Text;
Properties.Settings.Default.Save();
}
当字段值更改时,这就是`SaveSettings()的调用方式:
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Name' field.
private void textBox1_TextChanged(object sender, EventArgs e)
{
SaveSettings();
UpdateRichTextbox();
}
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Password' field.
private void textBox2_TextChanged(object sender, EventArgs e)
{
SaveSettings();
UpdateRichTextbox();
}
UpdateRichTextbox()
只需将Name
和Password
以格式化的方式写到富文本框中,这样我就可以看到它们的值:
// Writes current values of each setting to Rich Textbox
private void UpdateRichTextbox()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Name: {Properties.Settings.Default.Name}");
sb.AppendLine($"Password: {Properties.Settings.Default.Password}");
richTextBox1.Text = sb.ToString();
}
我确保将“项目设置”中的Name
和Password
设置都设为set the scope。我在.Save()之后尝试了calling Properties.Settings.Default.Upgrade()。
Name
的内容移到那些字段更改时调用的回调中时, Password
和SaveSettings()
仍然存在:
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Name' field.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Name = textBox1.Text;
Properties.Settings.Default.Save();
UpdateRichTextbox();
}
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Password' field.
private void textBox2_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Password = textBox2.Text;
Properties.Settings.Default.Save();
UpdateRichTextbox();
}
我不确定为什么我的第一种方法不起作用。 API开发人员打算如何使用.Save()
?
using System;
using System.Text;
using System.Windows.Forms;
namespace LoginForm
{
public partial class Form1 : Form
{
// Called initially when the form application starts up
public Form1()
{
InitializeComponent();
LoadSettings();
}
// Load saved values to their respective field textboxes
private void LoadSettings()
{
textBox1.Text = Properties.Settings.Default.Name;
textBox2.Text = Properties.Settings.Default.Password;
}
// Writes current values of each setting to Rich Textbox
private void UpdateRichTextbox()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Name: {Properties.Settings.Default.Name}");
sb.AppendLine($"Password: {Properties.Settings.Default.Password}");
richTextBox1.Text = sb.ToString();
}
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Name' field.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Name = textBox1.Text;
Properties.Settings.Default.Save();
UpdateRichTextbox();
}
// Saves ALL field values in the form whenever the field changes.
// Warning: This function gets called each time a character is added to the 'Password' field.
private void textBox2_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Password = textBox2.Text;
Properties.Settings.Default.Save();
UpdateRichTextbox();
}
}
}
答案 0 :(得分:0)
应用程序重新启动时,Form1()
调用LoadSettings()
,该textBox1.Text = Properties.Settings.Default.Name
在执行textBox1.Text
时修改textBox1_TextChanged
属性,从而使SaveSettings()
处理程序运行。该处理程序函数调用Properties.Settings.Default.Password
,然后使用textBox2.Text
字段中的当前内容覆盖先前存储在textBox2.Text
中的内容。因为应用程序已重新启动,所以Properties.Settings.Default.Password
为空,因此user.config
的旧值被空字符串覆盖。
感谢Jimi帮助我找到textChanged
文件,并就如何在textBox
事件中不要做什么提出了明智的建议。我看不出有一个很好的理由说明为什么每次Apply
中的字符更改时都需要做一些事情-解释为什么大多数应用程序使用Save
或accordion
按钮。在逐步调试时看到此文件更改,帮助我确定了代码问题。