Select to view content in your preferred language

Use an app.config file with an ArcGIS Pro Add-In

5627
14
Jump to solution
03-20-2018 05:51 AM
BillMacPherson
New Contributor III

Has anyone successfully used an app.config file with your Add-In? I have added an app.config file to my visual studio pro SDK project and I’m trying to retrieve a connection string from it. The retrieval code always returns null. I don’t know if that’s because it is a UserControl or running inside ArcGIS Pro or what. Any thoughts? Is there a better place to store the connection string?

 

Visual Studio 2017, .Net Framework 4.6.1, ArcGIS Pro SDK 2.1, SQL Server

 

app.config

 

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

<configuration>

    <connectionStrings> 

        <add name="my_conn" connectionString="Data Source=xx.xx.xx.xxx;Initial Catalog=some_db;User ID=xxx;Password=yyy" /> 

    </connectionStrings>

</configuration>

 

c# code

 

// Assume failure.

string returnValue = null;

 

// Look for the name in the connectionStrings section.

ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["my_conn"];

 

// If found, return the connection string.

if (settings != null)

    returnValue = settings.ConnectionString;

0 Kudos
14 Replies
Asimov
by
New Contributor III

@BillMacPherson my solution for being able to setup something similar to a Settings.settings .net file (so a typed config settings file) in an addin is to just design it in json: just add a json file in your project and set it up with Build Action "Content" and Copy to output directory "Copy always". In the json you can put any typed variable you need, then you provide a model class where you can deserialize the json into with a very simple function, such as:

internal static ConfigSettings InitializeConfigSettings()
{
    try
    {
        //file name is settings.json and it's inside a subfolder named Resources, you can change those name in any way
        string settingsPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Resources", "settings.json");
        if (!File.Exists(settingsPath))
            throw new ApplicationException($"Config file <{settingsPath}> not found");

        string jsonContent = File.ReadAllText(settingsPath);
        return JsonConvert.DeserializeObject<ConfigSettings>(jsonContent);
    }
    catch (Exception ex)
    {
        Logger.Error("Error retrieving settings", ex);
        throw;
    }
}

In this example I deserialize json content with newtonsoft.json into my model class, ConfigSettings, which I assign to a static global variable to use across the application.

The nice thing with this solution is that it is quite easy and fast to setup compared to the "official" solution, but still it allows you to change your settings "on the fly": you can do it by extracting the json file from the addin, change what you need, and then copy the file back in the addin (you can do it easily adding .zip extension to the addin). No recompilation is necessary and if you just have a bunch of setting values to change you can do it in a very similar way to what you are accustomed to do in .net applications outside addins.

0 Kudos
arunepuri1
New Contributor III

Hi All,

I am using a config.JSON file with ArcGIS Pro to read some user-defined values (like FME workbench paths and it's related input values). Which can be changeable based on user environment and user requirement.

But when I installed this add-in another system, the user will change the .json file according to his local path and values.

How Can make this .json file external exposed and editable by the user.

Please may I know, how to use/Change content in JSON and add-in should read the data from .json.

Thanks,

Arun E

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Your requirements are a bit confusing.  If you really need "user-defined parameters" then Charlie's step-by-step guide is exactly what you need and simple to implement.  Check under this section: https://github.com/Esri/arcgis-pro-sdk/wiki/ProGuide-Custom-settings#step-4-add-a-settingssettings-f...  In essence you use the standard recommended .Net implementation for your user settings (make sure to use user scope).  Below is a summary of the essentials:

User/Application Settings

0 Kudos
MarvisKisakye1
Occasional Contributor

Wolfgang Kaiser‌ I would like to add a new setting of type ArcGIS.Core.Geometry.LinearUnit but it's not available as an option in the settings file designer. Is there a way to add esri types in the .settings file?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

The easy way is to use the LinearUnit.Wkt property to get the 'well known text' of the linear unit and store the wkt string instead of the LinearUnit type.  At the point of retrieval you can then use CreateLinearUnit with the wkt text as an input parameter.