Apply Different Configurations
- 3 min to read
The eXpressApp Framework generates configuration files for both Windows Forms (App.config) and ASP.NET Web (Web.config). These files contain information that an application reads at runtime such as database connection strings, modules list, paths to important folders, and so on. In Windows Forms applications, some configuration parameters can be different for different application solution modes (e.g., Debug or Release). For instance, the UserModelDiffsLocation and TraceLogLocation parameters must be set to the ApplicationDataFolder value when managing the application life cycle via the ClickOnce technique. Thus, you have to modify configuration files each time an application is built in another mode. You can also create configuration files for each possible application solution mode, and copy it to the application project when needed. However, both of these approaches are inconvenient. This topic shows two convenient approaches to using the appropriate configuration files in different scenarios.
Specify a Configuration File via the Project Properties Window
Via this approach, you can create various configurations and use them as needed. For instance, a Windows Forms application can have the following configuration files:
Used when debugging the application.
Used to build a release version.
Used when testing the application installation.
Used for application deployment.
All configuration files must be called in the following manner: App.<ConfigurationName>.config.
Place all of these configuration files in the same folder. To specify which file must be used, do the following:
- In the Solution Explorer, right-click the Application project name and click Properties.
- Select the BuildEvents tab in the Project Properties window.
- In the Pre-build events command line box, insert the following: xcopy "$(ProjectDir)App.$(ConfigurationName).config" "$(ProjectDir)App.config" /Y /R
- Save the project.
Use Conditional Compilation
Via this approach, you can define two application configurations: Debug and Release. Use the DEBUG constant within the #If...Then...#Else directive to override configuration parameters when debugging an application. The following code demonstrates how to get the connection string specified by the ConnectionStringForDebug setting, when debugging the application:
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringForDevelopment"].ConnectionString; #if DEBUG string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringForDebug"].ConnectionString; #endif
In the code above, the connection string is specified by the ConnectionStringForDevelopment setting of the configuration file when the application is compiled in Release mode, and by the ConnectionStringForDebug setting when the application is compiled in Debug mode. To specify Release or Debug mode, use the Define DEBUG constant check box in the Build tab of the application project's Project Properties window. If this check box is in effect, the application is compiled in the Debug mode.
The DBUpdater updates the database if the connection string is specified by the ConnectionString setting in the configuration file. If you use another name for this setting, the DBUpdater will not work.