2017-06-03 67 views
-2

我正在尝试创建更新配置文件的应用程序。单击事件来更新文件C#

我已经做了以下形式:

enter image description here

我也创建了下面的代码:

using System; 
using System.IO; 
using System.Windows.Forms; 

namespace DebugOnOrOff 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string text = File.ReadAllText("C:\\Users\\User\\Desktop\\app.config"); 
     text = text.Replace("DebugOff", "DebugOn"); 
     File.WriteAllText("app.config", text); 

    } 
} 

}

+0

问题是? – kennyzx

+1

你做错了,有指定的方式来更新应用程序设置。查看[this]的第一个答案(https://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application)question –

+1

可能重复[Best练习在Windows窗体应用程序中保存应用程序设置](https://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) – Igor

回答

0
File.WriteAllText("app.config", text); 

应该要么

File.WriteAllText("C:\\Users\\User\\Desktop\\app.config", text); 

或者

File.WriteAllText(@"C:\Users\User\Desktop\app.config", text); 

作为一个侧面说明,你也可以更换此

File.ReadAllText("C:\\Users\\User\\Desktop\\app.config"); 

With

File.ReadAllText(@"C:\Users\User\Desktop\app.config"); 
+1

虽然在技术上是正确的,但有更好的方法来更新应用程序设置。 –

0

File.WriteAllText("app.config", text);

应该是:

File.WriteAllText("C:\\Users\\User\\Desktop\\app.config", text);

+1

尽管技术上正确有更好的方法来更新应用程序设置。 –

+0

@PeterBons – mjwills