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.

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.

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.

After that is completed. Voilá!

Thank you for reading, until next time.

Leave a Reply