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;
Solved! Go to Solution.
Here is what i do in my code. I am sure there is a better way to get the path to your app.config, but here is what i do. i create a settings class that is used to retrieve all the values from the App.Config. I set it up once and I am able to call it from everywhere. In the below example I initialize the appconfig Configuration mConfig and create public procedures to retrieve the values using it and the App.Config. You can change the AppConfigpath below to match where the relative location in the output directory. Hope this helps.
//APP.CONFIG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<appSettings>
<!--Set the Environment-->
<add key="Environment" value="TR"/>
</appSettings>
</configuration>
//Settings Class used to retrieve values from App.Config
public static class Settings
{
private static Configuration mConfig;
static Settings()
{
initializeAppConfig();
MessageBox.Show(CurrentApplicationEnvironment);
}
//CurrentApplicationEnvironment
private static string _CurrentApplicationEnvironment;
public static string CurrentApplicationEnvironment
{
get
{
if (string.IsNullOrEmpty(_CurrentApplicationEnvironment))
{
_CurrentApplicationEnvironment = mConfig.AppSettings.Settings["Environment"].Value;
}
return _CurrentApplicationEnvironment;
}
}
const string appConfigPath = "App.Config";
private static void initializeAppConfig()
{
try
{
string executingAssemblyFqPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
executingAssemblyFqPath = (System.IO.Path.Combine(executingAssemblyFqPath, appConfigPath));
if (System.IO.File.Exists(executingAssemblyFqPath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = executingAssemblyFqPath;
mConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
}
catch (Exception e)
{
}
}
}
On the App.Config do you have the Build Action set as "Content" and the Copy to Output Directory set to "Copy Always"?
I use the App.Config to store my web service URLs in the App Settings section, much like you do for the Connection string above.
Thank you for your quick replay. I'm glad to hear that using an app.config is possible.
Ok. I did as you suggested and set the Build Action to "Content" and the Copy to Output Directory to "Copy Always" but it didn't seem to help.
I also tried using <appSettings> and it didn't find that setting either. Is there anything special in the way you Get the setting? I tried in debug mode and as a compiled Add-In with no difference.
connectionString = System.Configuration.ConfigurationManager.AppSettings.Get("pd_apps_conn");
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="pd_apps_conn" value="Data Source=xx.xx.xx.xxx;Initial Catalog=some_db;User ID=xxx;Password=yyy" />
</appSettings>
<connectionStrings>
<add name="pd_apps" connectionString="Data Source=xx.xx.xx.xxx;Initial Catalog=some_db;User ID=xxx;Password=yyy" />
</connectionStrings>
</configuration>
Here is what i do in my code. I am sure there is a better way to get the path to your app.config, but here is what i do. i create a settings class that is used to retrieve all the values from the App.Config. I set it up once and I am able to call it from everywhere. In the below example I initialize the appconfig Configuration mConfig and create public procedures to retrieve the values using it and the App.Config. You can change the AppConfigpath below to match where the relative location in the output directory. Hope this helps.
//APP.CONFIG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<appSettings>
<!--Set the Environment-->
<add key="Environment" value="TR"/>
</appSettings>
</configuration>
//Settings Class used to retrieve values from App.Config
public static class Settings
{
private static Configuration mConfig;
static Settings()
{
initializeAppConfig();
MessageBox.Show(CurrentApplicationEnvironment);
}
//CurrentApplicationEnvironment
private static string _CurrentApplicationEnvironment;
public static string CurrentApplicationEnvironment
{
get
{
if (string.IsNullOrEmpty(_CurrentApplicationEnvironment))
{
_CurrentApplicationEnvironment = mConfig.AppSettings.Settings["Environment"].Value;
}
return _CurrentApplicationEnvironment;
}
}
const string appConfigPath = "App.Config";
private static void initializeAppConfig()
{
try
{
string executingAssemblyFqPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
executingAssemblyFqPath = (System.IO.Path.Combine(executingAssemblyFqPath, appConfigPath));
if (System.IO.File.Exists(executingAssemblyFqPath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = executingAssemblyFqPath;
mConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
}
catch (Exception e)
{
}
}
}
Thank you M Ka. Your was solution worked very well.
Hi, Are you able to change the settings in App.config after deploying.
I was not able to change the DB connection string after deploying. It still takes the old connection but not new connection string.
Any thoughts on this in using the App.config in arcgis pro add ins???
Hi Naresh,
Not found the proper solution and I gone with separate builds for Dev, Test and Prod.
Thanks Arun,
I am keeping the app.config in project path and using below code to get the DB conn string and we can change the connection string whenever we need.
string PrjPath = Project.Current.URI;
System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(PrjPath);
string executingAssemblyFqPath = directoryInfo.FullName;
Please consult: https://github.com/Esri/arcgis-pro-sdk/wiki/ProGuide-Custom-settings. It contains step by step instructions.
Thank you Charles. This is not really the direction I wanted to go for a solution.