Search code examples
c#application-settings

Can you change a setting in the Foo.settings file at run-time?


I have the following code that determines if it's the first time the user opens the app. If it is, a file dialog shows, and then it's supposed to change the setting to false, so it doesn't happen after that one time.

    public void VerifyIfFirstTimeRun()
    {
        if (Properties.Settings.Default.FirstTimeUse == true)
        {
            LocateWoWFolder();
            Properties.Settings.Default.FirstTimeUse = false;
        }                        
    }

In the last line, I receive the following error:

Error 1 Property or indexer 'CDLauncher.Properties.Settings.FirstTimeUse' cannot be assigned to -- it is read only

How can I do this?


Solution

  • I found out you can indeed change the .settings at run-time, but only setting in the "User" scope.

    So if you're trying to make a setting that does X only when the first time the app opens, you can do the following:

    public void VerifyIfFirstTimeRun()
    {
        if (Properties.Settings.Default.FirstTimeUse == true)
        {
            //Do something here.
    
            //Change first time usage Bool to false
            Properties.Settings.Default.FirstTimeUse = false;
    
            //Save your changes, and you're done!
            Properties.Settings.Default.Save();
        }                        
    }