Q.

How can an application configuration file (myapp.exe.config) be distributed with a ZTD application? In VS .NET, I've tried changing the app.config file's Build Action to Embedded Resource, but code like this still fails:

Dim settings As New System.Configuration.AppSettingsReader()
Dim o As Object = settings.GetValue("teststring", GetType(String))
MessageBox.Show(o.ToString())

I think my real question may be "how do I access an embedded config file?" Given that ZTD makes the embedding necessary, I thought you might have the answer off the top of your head.

Asked by Andrew Brust. Answered by the Wonk on January 21, 2003

A.

[download the sample code]

 

The AppSettingsReader class will only provide settings from an applications .config file and not from an embedded resource. To test your code, I created a .config file like so:

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="teststring" value="My Test String" />

  </appSettings>

</configuration>

 

I named it app.config and added it to my VB.NET project, which causes it to be renamed to make the name of the project and copied to the appropriate directory so that the .exe and the .config are in the same place, as shown in Figure 1.

 

[Image]
Figure 1: An application and its .config file

 

I took your source and dropped it into the Load event handler on the main form and got what’s shown in Figure 2.

 

[Image]
Figure 2: A string from the appSettings section of the .config file

 

The AppSettingsReader class reaches into the application’s .config file, parses the <appSettings> element to provide name/value pairs made available with the GetValue method. It gets the name of the file to read from the application domain data, specifically the APP_CONFIG_FILE data returned from the GetData method of the current AppDomain:

 

MessageBox.Show(System.AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE"))

 

The APP_CONFIG_FILE data will contain a path to the .config file relative to the application base (available by passing “APPBASE” to GetData) and based on the name of the .exe itself, e.g. VbSmartClient.exe.config.

Enabling .config Download for Smart Clients

While the .config file will always be based on the name of the application, the actual path to the .config file won’t always be to a file on your hard drive. In the case of a smart client, i.e. a WinForms application launched via an URL, the .config file will be accessed via an URL because the APPBASE will be the URL from which the application was downloaded. To test this, I right-clicked on the bin folder created by VB.NET that contained the .exe and the .config files for my smart client and chose Properties and then the Web Sharing tab. I named the web share ConfigFun and tested smart client launching with the following URL:

 

http://localhost/ConfigFun/VbSmartClient.exe

 

Unfortunately, this caused my application to fail as shown in Figure 3.

 

[Image]
Figure 3: .config not available by default for smart clients

 

This happened because, by default, ASP.NET disables download of any kind of source file from your web site, including any .config files. This is good, because it protects your web.config file, but bad because it disables your application’s .config file from being downloaded. To enable this, you need the following minimal web.config file at the root of your web share folder:

 

<configuration>

  <system.web>

    <!--

    Remove the *.config handler so that we_can serve up

    *.exe.config files, but make it forbidden to serve up the

    web.config file itself.

    -->

    <httpHandlers>

      <remove verb="*" path="*.config" />

      <add verb="*" path="web.config"

        type="System.Web.HttpForbiddenHandler"/>

    </httpHandlers>

 </system.web>

</configuration>

 

This web.config configures your ASP.NET web application to remove the restriction on downloading .config files, but to enable the restriction for web.config files specifically. This lets your application’s .config file through, but stops your web.config file from being available to any casual observer.

 

Unfortunately, that darn message box still hasn’t gone away. One more step is needed. Out of the box, IIS doesn’t allow .config files to be downloaded unless anonymous access is enabled. So, to enable anonymous access for the ConfigFun web share, choose Start->Programs->Administration Tools->Internet Information Services, find the ConfigFun web share, right-click and choose Properties, choose the Directory Security tab and enable Anonymous access, as shown in Figure 4.

 

[Image]
Figure 4: Enabling anonymous access for a web share

 

Once you’ve done this last tweak, you’ll all set. The .config file will be downloaded along with your application and your AppSettingsReader will be happy.

How I Figured This out

This non-trivial set of steps came from observation, experimentation and complaining loudly (and falsely) of a bug on public mailing lists until someone told me how to do it.

Smart Client Names

Andrew introduced me to a new way to refer to smart clients with his question: ZTD. It took me a second to figure out that this meant Zero Touch Deployment application, bringing the ways that the community has to refer to this technology to four that I know of:

 

Personally, I prefer the term “smart client” in public, as it sounds so much better than “thick client”, but I find myself using href-exe amongst my geekier friends. I don’t really care what we call it; I’m just cataloging the terms so that we’re all on the same page.

Where Are We?

Three things are needed to download the .config file with the application:

1.      The .config file needs to be in the same APPBASE aka folder as the .exe it’s meant to configure

2.      The minimal web.config settings need to be in place to enable ASP.NET to serve up .config files

3.      The IIS web share needs to be configured to allow .config files

 

Once this is done, as the .config file changes, the updated .config file will be downloaded and cached automatically, letting you use the .config file as a way to configure applications on user’s machines w/o manually deploying configuration information, just as a smart client itself is automatically deployed.

References

Feedback

I have feedback on this Ask The Wonk answer