App.Config file is not getting red in my plug-in (C# 1.1, Excel 20

M

Mike

Hi! I copied app.config file from my WinForm application that works fine. I
renamed the file as myAddIn.dll.config and placed it in every folder where
the setup installs myAddIn.dll. Now I am trying to read the settings and
constantly getting Null like the config file is not red at all.

ConfigurationSettings.AppSettings
string configFileURL =
(string)ConfigurationSettings.AppSettings["CONFIGFILEURL"];

The code above works fine in my desktop application but does not work in
Excel add-in.

Please help.

Thank you in advance,

--Mike
 
B

- B@rney

Since the add-in is actually running in the context of Excel, the only way to
automagically load an app.config file would be to name it Excel.exe.config,
and place it next to Excel. But then it's shared for all add-ins.

What I did was I placed a file in the same directory as my add-in .dll and
used the following code to load it...

To load the value of a key from myAddIn.config called connStr into a string
variable called strConn:

You need to be:
using System.Xml;
// Create a XML document to hold the config file
XmlDocument configDoc = new XmlDocument();

// Read the addin.config
configDoc.Load(Path.Combine(<path to config file>,
"myAddIn.config"));

// Get the value
XmlNode valueNode =
configDoc.SelectSingleNode("configuration/appSettings/add[@key='connStr']");
string strConn = valueNode.Attributes["value"].InnerText;

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top