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 set up my addin to read folder name and path from app.config file.using:
ConfigurationManager.AppSettings
to get the value. This is giving me an error.
[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:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroupname="userSettings"type="System.Configuration.
UserSettingsGroup, System, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<sectionname="BabuRevitAPI.HCL"type="System.Configuration.
ClientSettingsSection, System, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"requirePermission="false" />
<sectionname="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>
<settingname="pdfOutput"serializeAs="String">
<value>pdf outt put folder</value>
</setting>
<settingname="auslocal"serializeAs="String">
<value>aus local folder</value>
</setting>
<settingname="test"serializeAs="String">
<value>I am a test</value>
</setting>
</BabuRevitAPI.HCL>
</userSettings>
</configuration>
Error Image after using above code.
Answer: There is a couple of steps we need to follow in order 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. Where 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 other than that 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.
[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 .config file.
<?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 gets installed.
After that is completed. Voilà!
Thank you for reading, until next time.