简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM