Get Value from App.config file and Sur la France

By Jaime Rosales

After a great end of August, where the wedding was a total success, I now have a gorgeous wife for the rest of my life. The honeymoon vacation was incredible. The culinary experience in Iceland and France was unforgettable. I’m now back as a married man, wiser on the wine culture of France, and 100 times more in love with black truffles. My batteries have fully recharged and I’m excited to be back.

Balance Rocks French Riviera

The question this time came from our Autodesk Forum.

http://forums.autodesk.com/t5/revit-api/get-value-from-app-config-file/m-p/5244679#M7119

Question: I am trying to set up my addin to read a folder name and path from an app.config file using:

ConfigurationManager.AppSettings

to get the value. This is giving me an error. Here is the code that was giving the error:

Original Code (Failing)


[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
    public Result Execute(
        ExternalCommandData commandData,
        ref string message,
        ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Application app = uiapp.Application;
        Document doc = uidoc.Document;

        string dir = ConfigurationManager.AppSettings["pdfOutput"];
        TaskDialog.Show(dir, dir);

        return Result.Succeeded;
    }
}

Here is the XML file being used with the failing code (Note: the original XML was highly nested and not using the simple <appSettings> structure):

Original XML Configuration (Failing)


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="BabuRevitAPI.HCL" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="BabuRevitAPI.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <BabuRevitAPI.HCL>
            <setting name="pdfOutput" serializeAs="String">
                <value>pdf outt put folder</value>
            </setting>
            <setting name="auslocal" serializeAs="String">
                <value>aus local folder</value>
            </setting>
            <setting name="test" serializeAs="String">
                <value>I am a test</value>
            </setting>
        </BabuRevitAPI.HCL>
    </userSettings>
</configuration>

Error Image after using above code.

image

Answer: There are a couple of steps we need to follow to solve this and not get our value = null like it’s happening in this case. Here is a snippet of code that came from one of our evangelists Augusto Goncalves. We have to keep in mind that the config file needs to be at the same location and with the exact name as the DLL of our addin. This needs to be taken into consideration; otherwise, the value will be null since it will be reading the AppSettings Keys from the Revit.exe.config file where the 4 existing keys will not match our custom one.

Corrected Code (Working)


[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
    public Result Execute(
        ExternalCommandData commandData,
        ref string message,
        ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Application app = uiapp.Application;
        Document doc = uidoc.Document;

        Configuration config = ConfigurationManager.OpenExeConfiguration
            (System.Reflection.Assembly.GetExecutingAssembly().Location);
        string value = config.AppSettings.Settings["pdfOutput"].Value;
        TaskDialog.Show("App config Value", value);

        return Result.Succeeded;
    }
}

Here is the much simpler .config file that works with the solution code (using the standard <appSettings> section):

Corrected XML Configuration


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="pdfOutput" value="Yes, I'm Working"/>
    </appSettings>
</configuration>

As you notice the code in the answer is much simpler, but keep in mind that is just for testing purposes.

Here is how the name should be placed at the location where our addin files get installed.

Screen Shot 2014-09-03 at 5.59.00 PM

After that is completed. Voilá!

Screen Shot 2014-09-03 at 6.03.05 PM

Thank you for reading, until next time.


Comments

2 responses to “Get Value from App.config file and Sur la France”

  1. Hi Jaime,
    Thanks for your solution with sample code.
    Will you please explain how to get the value form Settings
    http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
    with the settings you can have string, integer etc
    Regards
    Babu

  2. Hi Babu,
    I was about to start explaining this, when I ran into this amazing post explaining all the questions you may have regarding the use of Settings or AppSettings. Hope it helps.
    “All the configuration setting values are stored in string format in the appSettings section of an application’s configuration file.”
    “But it gets a little more complex if you need to grab a value that’s not a string. For numeric or enum values you need to first ensure the value exists (is non-null) and then convert the string explicitly to whatever type, since configuration values are always strings.”
    http://weblog.west-wind.com/posts/2012/Dec/28/Building-a-better-NET-Application-Configuration-Class-revisited
    Cheers,
    Jaime

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading