为什么在调用Properties.Settings.Default.Saved()之后不能保留我的用户设置?

时间:2019-05-29 15:17:44

标签: c# winforms settings

这仅用于演示目的。我已经收到有关应用程序设置不安全的建议,实际上我不会以这种方式缓存密码。

问题陈述

我正在创建一个登录Windows Forms应用程序,该应用程序在使用Application Settings的应用程序会话之间保留在NamePassword字段中输入的字符串值。表单应加载从上一个会话输入的NamePassword值。

我遇到一个问题,如果我使用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()只需将NamePassword以格式化的方式写到富文本框中,这样我就可以看到它们的值:

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

尝试

我确保将“项目设置”中的NamePassword设置都设为set the scope。我在.Save()之后尝试了calling Properties.Settings.Default.Upgrade()

当我将Name的内容移到那些字段更改时调用的回调中时,

PasswordSaveSettings()仍然存在:

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

1 个答案:

答案 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中的字符更改时都需要做一些事情-解释为什么大多数应用程序使用Saveaccordion按钮。在逐步调试时看到此文件更改,帮助我确定了代码问题。