Different application settings depending on configuration mode

后端 未结 6 1894
生来不讨喜
生来不讨喜 2020-12-08 05:15

Is anyone aware of a way that I can set application (or user) level settings in a .Net application that are conditional on the applications current development mode? IE: Deb

6条回答
  •  眼角桃花
    2020-12-08 05:54

    I know this was asked years ago, but just in case anyone is looking for a simple and effective solution that I use.

    1. Go to project properties, Settings tab (you'll see your web service URL or any other settings already listed here).

    2. Click the "View Code" button available on the Settings page.

    3. Type this in the constructor.

      this.SettingsLoaded += Settings_SettingsLoaded;
      
    4. Add the following function under the constructor:

      void Settings_SettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e)
      {
          #if(DEBUG)
          this["YOUR_SETTING_NAME"] = VALUE_FOR_DEBUG_CONFIGURATION;
          #else
          this["YOUR_SETTING_NAME"] = VALUE_FOR_RELEASE_CONFIGURATION;
          #endif
      }
      

    Now whenever you run your project, it will compile only the line that matches the current build configuration.

提交回复
热议问题