-2

I have a checkbox(Name:tarahi_algouritm) and a button(Name:button1) on my form(Name:frm_choose).I want to save the latest changes on my checkbox as user clicked on the button.it means user run the program and check the checkbox and then click on button and then close the program.when he/she Rerun it,checkbox should be checked.or someway he disable the checkbox and click on button and after another run,checkbox should be disabled.

for this, in application setting(table part) put a checkbox (Name:s_tarahi_algouritm)and choose USER in scope part..as I said changes are apply on checkbox and s_tarahi_algouritm is used for save the latest changes on checkbox.I wrote these codes:

private void frm_choose_Load(object sender, EventArgs e)
    {
        if (Properties.Settings.Default.s_tarahi_algouritm!=null)
          tarahi_algouritm= Properties.Settings.Default.s_tarahi_algouritm;

    }

private void button1_Click(object sender, EventArgs e)
    {
        Properties.Settings.Default.s_tarahi_algouritm = tarahi_algouritm;
         Properties.Settings.Default.Save();
    }

but When I make changes on checkbox and close the debug and Rerun it,changes are not applied. what should I do?where is wrong?I am partly beginner so explain explicit. thank you all

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Behnam
  • 1
  • 2
  • 5
    I suggest you only save the value of `CheckBox.IsChecked`. Seems quite unusual to save a whole `CheckBox` in the settings. – wkl Feb 14 '17 at 12:50
  • i have a lot of checkbox.all the information I said above must be the same on them.say your solution explicit rather by code – Behnam Feb 14 '17 at 13:05

2 Answers2

0

I tested many things like:

Properties.Settings.Default.Properties.Add(new System.Configuration.SettingsProperty("a"));
        Properties.Settings.Default.Properties["a"].DefaultValue = "b";
        Properties.Settings.Default.Save();

it has not error but do not save. In this link:

C# Settings.Default.Save() not saving?

Answered you must add Properties.Settings.Default.Reload(); after saving, I did that but not changed. It seems no one knows the answer.(I read many articles).

It looks like a cancer to me! I suggest you to easily save your settings to a xml file.

Below i add an easy xml saving method:

using System.Xml.Linq;

And

        XElement settings;
        try
        {
            settings = XElement.Load("settings.xml"); //beside the app .exe file
        }
        catch (Exception) // it is first time and you have not file yet.
        {
            settings = new XElement("settings");
            settings.Save("settings.xml");
        }

If you want to add new element:

        settings.Add(new XElement("firstKey", tarahi_algouritm.Checked.ToString()));
        settings.Save("settings.xml");

If you want to read or edit element:

        XElement settings = XElement.Load("settings.xml");  
        string firstKey = settings.Element("firstKey").Value; //reading value

        settings.Element("firstKey").Value = "New Value";  //Edit
        settings.Save("settings.xml");   //Save

Remember that firstKey is only a name and you can use another names instead.

Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
  • here is the error: An unhandled exception of type 'System.Configuration.SettingsPropertyWrongTypeException' occurred in System.dll Additional information: The settings property 's_tarahi_algouritm' is of a non-compatible type. – Behnam Feb 14 '17 at 13:23
  • Does your project have a setting? I mean did you add setting to your app? – Farzin Kanzi Feb 14 '17 at 13:34
  • Please have a look on this: http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application – Farzin Kanzi Feb 14 '17 at 13:41
  • Please give me a few minutes time to find the problem. – Farzin Kanzi Feb 14 '17 at 13:46
  • Thanks for searching!اره عزیز دل حتما چرا که نه یه منبع خوب و روون که با xml بشه این کار رو انجام داد – Behnam Feb 14 '17 at 15:21
  • @Behnam, I updated the answer and added xml solution. – Farzin Kanzi Feb 14 '17 at 15:59
  • thanks a lot man,i just understand the codes that is written a comment front of it.where should i write these codes exactly?i want to save changes on a checkbox that the name of it is tarahi_algouritm.what should i do exactly?explain it on the code that you wrote.i don't understand the connection between"firstvalue" and a checkbox too! @ – Behnam Feb 21 '17 at 10:31
  • Why do you say it is not answer? – wkl May 23 '17 at 11:14
  • @wkl ... Ok I removed that. Thanks. – Farzin Kanzi May 23 '17 at 12:06
0

The problem is the settings files are written out in two parts: one to the application settings (which you can't save to) and the other to the user settings (which you can save to). You need to save the user settings (it gets written to your c:\users{userid}... directory).

Look at the most up-voted response to Farzin's link. It explains the issue as well.

Here's a more thorough explanation: App.config: User vs Application Scope

Here's an example.

I created a webform app and added a Settings file to it (called TestSettings.settings). I added two values:enter image description here

When you run this application it creates a file in the application directory named the same as your executable with .config appended that contains (among other things) a element and a element. But this file only contains the initial values. If you change the value under the element and call Save() it will not update this file. It will create a file:

c:\Users{username}\AppData\Local{appname}{random_dir_name}{version}\user.config

My code to demonstrate this was:

Console.WriteLine(TestSettings.Default["UserValue"]);
TestSettings.Default["UserValue"] = "def";
TestSettings.Default.Save();
Community
  • 1
  • 1
dviljoen
  • 1,612
  • 1
  • 16
  • 28
  • HI @dviljoen. In all of the links (I flowed to end) you can not see a live example. the first comment of one: *Any live example will be appreciated.* but there is only not comprehensive explanations. – Farzin Kanzi Feb 14 '17 at 15:33