Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存配置选项的app.config或TXT/INI?_C#_.net_App Config - Fatal编程技术网

C# 保存配置选项的app.config或TXT/INI?

C# 保存配置选项的app.config或TXT/INI?,c#,.net,app-config,C#,.net,App Config,我正在开发一个应用程序,用户应该在其中定义一个输出目录。在阅读了大量关于app.config和ini(表示不推荐使用,并且.NET没有任何类或名称空间来阅读它们)和txt(有点乏味)的内容后,我决定转到app.config。所以我写了这段代码: // Here I save to app.config file private void button1_Click(object sender, EventArgs e) { if (textBox2.Tex

我正在开发一个应用程序,用户应该在其中定义一个输出目录。在阅读了大量关于app.configini(表示不推荐使用,并且.NET没有任何类或名称空间来阅读它们)和txt(有点乏味)的内容后,我决定转到app.config。所以我写了这段代码:

    // Here I save to app.config file
    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox2.Text != "")
        {
            oConfig.AppSettings.Settings.Add("directorio_bases", textBox2.Text);
            oConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            textBox1.Text = ConfigurationManager.AppSettings["directorio_bases"];
        }
        else
        {
            MessageBox.Show("Debes seleccionar una ruta donde guardar las bases generadas", "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    // Here I read from app.config file just for informative purpose only
    private void ConfigurationUserControl_Load(object sender, EventArgs e)
    {

        textBox1.Text = ConfigurationManager.AppSettings["directorio_bases"];
    }

    // Here is where I get the path to the folder
    private void button2_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox2.Text = folderBrowserDialog1.SelectedPath;
        }
    }
代码在程序执行期间运行良好,但当我关闭/打开程序时,程序值丢失。所以我的问题是:

  • 我做错了什么?我的意思是为什么价值观不能保持
  • 您是否使用了app.config、config.ini或config.txt文件?我需要听听你对这个话题的看法

您是否尝试手动运行可执行文件(例如从bin文件夹)? 如果在调试模式下运行应用程序,则不会修改app.config文件

请查看以下链接: