-2

I am attempting to create an application which updates a config file.

I have made the following form:

enter image description here

I have also created the following code:

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);

    }
}

}

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
A Bob
  • 3
  • 3
  • and the problem is? – kennyzx Jun 03 '17 at 11:30
  • 1
    You are doing it wrong, there are specified ways to update app setting. See the first answer of [this](https://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) question – Peter Bons Jun 03 '17 at 11:31
  • 1
    Possible duplicate of [Best practice to save application settings in a Windows Forms Application](https://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) – Igor Jun 03 '17 at 11:32

2 Answers2

0

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

should be:

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

mjwills
  • 23,389
  • 6
  • 40
  • 63
0
File.WriteAllText("app.config", text);

Should either be

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

Or

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

As a side note you could also replace this

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

With

File.ReadAllText(@"C:\Users\User\Desktop\app.config");
Gareth
  • 913
  • 6
  • 14