I’ve done a couple of posts on using configuration files in web application
and WinForms applications. This week I needed to do the same thing from within
ArcMap, so I thought I’d whip up a quick sample that shows how to do this.
Many of you may know that ArcMap has a config file (ArcMap.exe.config). The
only thing that’s in there by default is the specification of the .NET runtime
that ArcMap will use.
<startup>
<supportedRuntime version=“v2.0.50727“ />
</startup>
[Note: ArcCatalog, ArcGlobe and ArcScene also have config files - this same technique can be used with app these applications]
However, it can be used just like any other config file – and that’s what
this sample will show.
This sample is about as simple as it can possibly be – it’s just going to pull a string out of a custom section. This is because I can’t share the real code I built this week, and I wanted to keep this really simple.
Modify ArcMap.exe.config
Open the file in text editor of choice, and drop in your custom section handler, and the section.
<configSections>
<section name=“simpledata“ type=“DaBo.ArcMapConfig.SimpleConfigHandler, DaBo.ArcMapConfig“ />
</configSections>
<!– I told you it was simple! –>
<simpledata value=“Some Config Data from ArcMap.exe.config“ />
A few things to note about this – in order for the framework to locate the config handler (this is not COM – there is no registry to rely on) the assembly must be local to the config file. This means that you’ll need to copy the config handler assembly into C:Program FilesArcGISBin or where ever you installed ArcGIS. I think you can also put the assembly in the GAC, but since it’sonly going to be used by ArcMap, there is no need.
During debugging, it is very easy to do this copy via a Post-Build Event. Open your Project properties and go to the Compile tab. At the bottom you’ll see a Build Events button…
Accessing the Config Data
The sample code shows two ways to access the configuration data. It does this via two ArcMap commands. The first simple accesses it via the ConfigurationManger class (hint: you need to add System.Configuration as a reference to get this class!). It simply pulls the data from the config and displays the string in a messagebox.
Public Overrides Sub OnClick()
‘Pull a value from the config file
Dim simpleData As String = SimpleConfigHandler.GetConfig
MessageBox.Show(“This is pulled from the Config File directly: “ + simpleData)
End Sub
The second command takes this a step further – it pulls the data from an ArcMap extension which loads the data from the config when it loads. This is a better example because you’ll likely be storing something useful in there (i.e. a connection string, application extension information etc). In that case you may want to expose in a more controlled manner – for example – if you are storing a connection string (which you have encrypted), you may want to hand out database connections instead of the actual connection string. In that case, having a central repository like an extension would be a very handy design decision.
When the extension starts up, it loads the data from the config, and stored it in a property…Public Sub Startup(ByRef initializationData As Object) Implements ESRI.ArcGIS.esriSystem.IExtension.Startup
_simpleData = SimpleConfigHandler.GetConfig
End Sub
Then the command simply gets a pointer to the extension, and pulls the value from a property on the extension.
Public Overrides Sub OnClick()
Dim simpleExt As DaBo.ArcMapExtension.SimpleExtension = m_application.FindExtensionByName(“SimpleExtension”)
MessageBox.Show(“This is pulled from the extension: “ + simpleExt.SimpleData)
End Sub
Summary
Although very simple, this sample should provide a starting point for using configuration sections with your desltop customizations. I would also note that you can create configuration files for class libraries – I’ve done this in the past for other projects, but somehow it just felt a little “wrong”. Since ArcMap has a config file, it’s nice to use it instead.