How to read your own config file with a VSTools Application

Dave Duesk - Click for blog homepageThis is a reposting of an article Dave Duesk originally wrote on my Developing for Dynamics GP blog.

This article is a result of a couple of recent support incidents where developers would like to read a config from from a Visual Studio Tools (VSTools) addin. VSTools is a special situation because for one, it is a dll, not an exe, and second it only runs if GP is running. VSTools can automatically access the dynamics.exe.config file, steps on how to do that are outlined in KB Article 933930 (Not available).  The KB is for 9.0 and references the Dynamics.config file, but it also works for the Dynamics.exe.config on 10.0.

In this case, we don’t want to use the dynamics.exe.config, we want to use our own config file. Since we are working with a dll, there is no automatic reading from config files like with an exe. Obviously, there is more than one way to handle this and there may be a better way.  This is just one way, that I know works, and it’s not too difficult.

This article shows a method to use xml to read the data from a config file. The main VSTools project is called DynamicsGPAddin.dll.

  1. The config file is named DynamicsGPAddin.dll.config and is deployed to the Addins folder in the main Microsoft Dynamics GP directory.  The config file contains the following information.  You can define this however you want, this just a sample config file.<?xml version=”1.0″ encoding=”utf-8″ ?>
    <configuration>
    <startup>
    <supportedRuntime version=”v1.1.4444″/>
    <requiredRuntime version=”v1.0.9999″/>
    </startup>
    </configuration>
  2. The name of the VSTools application is DynamicsGPAddin.dll and contains a form with a button and two text fields.  The button reads the config file and displays the data on the window fields.  In order for the code to execute, references to the following must be marked.System.Configuration
    System.XML
  3. The button code is as follows:

C# Code Example

DynamicsGPAddin

private void button2_Click(object sender, EventArgs e)
{ 
  //
  // Verify the configuration file exists.
  //
  string aConfigPath = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName + ".config";if (System.IO.File.Exists(aConfigPath) == false)
  {
    Application.ExitThread();
  }  

  //
  // Read the runtime versions.
  //
  XmlNode xmlNode;
  XmlNodeList xmlList;
  XmlDocument xmlDoc = new XmlDocument();
  string aRequiredVersion = "";
  string aSupportedVersion = "";

  //navigate the xmldoc to get to the node you want.
  xmlDoc.Load(aConfigPath);
  xmlNode = xmlDoc.DocumentElement.SelectSingleNode("startup");
  xmlList = xmlNode.SelectNodes("supportedRuntime");

  foreach (XmlNode xmlNodeS in xmlList)
  {
    //Get the attribute value
    aSupportedVersion += "," + xmlNodeS.Attributes.GetNamedItem("version").Value;
  }
  aSupportedVersion = aSupportedVersion.Substring(1);

  aRequiredVersion =
  xmlNode.SelectSingleNode("requiredRuntime").Attributes.GetNamedItem("version").Value;
  if (aRequiredVersion != "")
  {
    textBox2.Text = aRequiredVersion; //set a field on the form
  }

  if (aSupportedVersion != "")
  {
    textBox3.Text = aSupportedVersion; //set a field on the form
  }
}

Dave
MBS Dev Support

// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

This article was originally posted on the Developing for Dynamics GP Blog and has been reposted on http://www.winthropdc.com/blog.

3 thoughts on “How to read your own config file with a VSTools Application

  1. I realize it's silly to post to an article from 4 years ago, but I'm wondering why even in today's Visual Studio® Tools for Microsoft Dynamics® GP 2010 Programmer’s Guide there is no mention of this issue.  I wasted enough time before I managed to find this article, and I'm sure it might be of help to others new to using this tool for GP development.
    Thanks, Dave.

    Like

  2. From a .NET perspective, this is just a dll.  A dll (any – not just a GP addin assembly) does not have "automatic configuration file support" like an exe would so this kind of problem isn't specific to a vstools addin.  
    Also Dave points out a KB KB I wrote years ago  – because we _are_ running from an exec (Dynamics.exe) you could add your entries in the dynamics.exe.config file and then your settings would "just work" as if you assembly was executing as an exe.
    But all that said, I'm sure it wouldn't hurt to have something in the documentation that references either option.

    Like

Please post feedback or comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.