Saturday 23 September 2017

arcobjects - ArcMap Add-in with app.settings not recognizing app.config changes?


I have developed an ArcMap Add-in which requires a configuration file. After spending a while trying to read config values from a single App.Config file (and always getting null) I believe that the Add-in cannot read values from here as it is a class library, and looks for the calling application (ArcMap)'s config file when I ask for a key's value (hence the null).


To get around this I used an App.Settings file, which the application can read fine. Creating this also introduces an App.Config file into the environment and Visual Studio seems to keep the two files in-sync during development.


Now that the Add-in is being deployed I need to be able to change configuration values (e.g. log file location). I have tried opening / extracting the .esriaddin file and updating the App.Config file in there but the Add-in retains the same configuration values it had when compiled. I know that the new App.Config values are being persisted in the .esriaddin file because I can view them again after closing the archive.


Does anyone know a reliable way to configure an Add-in and permit this configuration to be updateable once deployed? Any suggestions very welcome as it seems ridiculous that I should need a custom config file for this.


App.Settings values are at the application level, and currently both App.Settings and App.Config have build action: none / do not copy.



Answer



I figured out how to configure the addin.


The addin file in ...Documents\ArcGIS\AddIns\Desktop10.0... gets expanded every time ArcMap loads, so the only place that ANY config files embedded in the addin can be edited is in here. I didn't experiment with using registry keys or using a dedicated addin configuration directory as this just seemed overkill.



In the end I used an app.config file (because even if used with a class library, which ignores the config file, it still gets renamed in line with the assembly and automatically included in the addin archive) for my settings. Based on a link provided above I used the following configuration class


...


    public AppConfig()
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = this.GetType().Assembly.Location + ".config";
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}

catch (Exception)
{
...
}
}

private string getValue(string key)
{
return config.AppSettings.Settings[key].Value;
}


...


To edit the config once the addin is deployed I had to close ArcMap, open the .esriAddIn file with winrar, go to \install and open the config file, edit it, close the editor, and then allow winrar to update the file within the archive. Then reloading ArcMap the change goes in. Annoyingly this is one of the first things I tried but I think I had problems because the editor for the config file was still open when winrar updated the archive.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...