One C# .NET application I’ve been working on for a while consists of two executables: a main application and a taskbar tray applet. The two apps needed to share common configuration settings, which I planned to implement via the app.config file. The way .NET is set up (I’m using .NET 1.1), each .EXE loads its config file based upon the executable’s file name. For example, xyz.exe loads xyz.exe.config. This is a problem when abc.exe needs to access the same config settings. Now, .NET may have some clever API to solve this problem, but I didn’t find one.
My answer to the situation was to create a file named common.config, which I placed within the project for a shared library. Inside this config file are the <appSettings> and </appSettings> tags, with the application’s key, value pairs coded inside:
<appSettings>
<add key="database_type" value="MSACCESS"/>
<add key="datasource" value="mydatabase.mdb"/>
<add key="default_browser" value="C:\Program Files\Internet Explorer\iexplore.exe"/>
</appSettings>
Once the common.config file was created, I needed to reference it from each executable’s app.config file. I accomplished this using the “file=” parameter of the <appSettings> tag. Below is my entire app.config file, which I duplicate in the projects for the main application and a taskbar tray applet:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
</appSettings>
</configuration>
Note that the above does not include path information for common.config. The reason for this is that common.config is installed on a user’s system in a single directory, along with the application executables. Thus, simply using the current directory is appropriate. However, this doesn’t work when using the Visual Studio development environment, where each part of the application is built in a different directory. So, for each executable to find this file during Debug execution, I added a Pre-Build Event to each executable’s project, which first copies common.config to that project’s directory:
copy $(SolutionDir)MyCommonDll\common.config $(TargetDir)
With this done, I now have a configuration file that is shared by each executable in my application.
No comments:
Post a Comment