tags:

views:

196

answers:

3

Here's my code:

public void VerifyIfFirstTimeRun()
    {
        if (System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] == "true")
        {
            //Do bla bla bla
            //Then do bla bla bla
            System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] = "false";     
        }            
    }

Problem is, I'm testing this with the F5 key, it boots up and sure enough using a breakpoint shows that it is indeed going inside the If Condition, but when I "Stop" the application and press F5 again it goes inside the If Condition AGAIN. Is this standard operational procedures?

If so, how can I test if its working?

+4  A: 

I'm not sure you should expect this to save; you can, however, have a settings file that has a setting (a bool in this case) in the user's context, which saves (when you ask it to) via Settings.Default.Save().

Marc Gravell
+4  A: 

This is going against the spirit of what the App.config file is used for ... but to answer your question, you need to do System.Configuration.Configuration.Save().

Edit:
App.config is typically used to configure database connections, providers, etc. And is usually set once at installation. It's suggested that user settings go into a separate config file user.config. See this for the explanation.

Edit:
System.Configuration.Configuration class.

Note - now that I read why you're using these settings, may I suggest another way? You should probably just check if the file is there:

if (!File.Exists("thefilepath"))
{
    AlertUserOfMissingFile();
    ChooseNewFile();
}

It's safer this way anyhow because having a setting set to true doesn't necessarily mean the file is where you think it is.

xanadont
D'oH! Well I'm here to learn :P Can you explain what the App.config file is actually used for? I was under the impression that this is it's use.
Sergio Tapia
It's used to provide application-level settings. If you want per-user settings you should add a Settings class to your project, define the relevant properties as user properties and interact with them via the Settings class as @Marc suggests.
tvanfosson
Well my small application will only check if it's the first time to launch just because I want to make sure the end-user selects a file with the FileDialog I'll use later on. I don't see a need for user-specific settings in this case, am I ok using the App.Config file for this?Also, regarding Xanadont's answer, "System.Configuration.Configuration.Save()" doesn't exist. The second ".Configuration" doesn't exist in the intellisense. Are you sure you wrote the line properly?Massive thanks for all the help.
Sergio Tapia
How can you be sure that the user has appropriate privileges to modify the app.config file? The only thing you can guarantee is that the user can save their own settings. It won't hurt anything to use per-user settings since each user will presumably have to make the selection anyway. If you only have one user, it still works.
tvanfosson
OK, I'll follow your advice. Would you mind pointing me in the right direction? A link or tutorial I could read regarding Per-User Settings? Thank you very much. :)
Sergio Tapia
You might try http://social.msdn.microsoft.com/Search/en-US?query=application+settings.
John Saunders
+1  A: 

I don't advice you to use App.settings for this purpose. Take a look a this article Settings in C#

  1. In Solution Explorer, expand the Properties node of your project.
  2. In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
  3. In the Settings designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single setting. Figure 1 shows an example of the Settings designer.

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();
Sorantis