ChrisB
ChrisB

Reputation: 907

Dynamically initializing WinForm app settings in C#

In my WinForm application (C# .NET 4.0) settings, I have a setting called EnvironmentName. It's a string of User scope. The initial value of this setting must be read from some file on the disk the first time the application starts. I also want my users to be able to change that setting to an arbitrary static string of their liking if need be.

My first thought was to initialize EnvironmentName to a blank string and then, in my main form's Load event, do something like this:

if (String.IsNullOrEmpty(Properties.Settings.Default.EnvironmentName))
    Properties.Settings.Default.EnvironmentName = GetEnvNameFromDisk();

But what's the recommended way to do this?

Upvotes: 0

Views: 270

Answers (1)

Tarik
Tarik

Reputation: 11209

Assuming you have created a user scoped property in the designer and assigned a value to it, the assigned value is hard-coded in the designer generated code. Accordingly, you do not need to check for null or empty. You can then save the new settings using

Properties.Settings.Default.Save();

Upvotes: 1

Related Questions