Using configuration file in Outlook Addin

Add-in Express™ Support Service
That's what is more important than anything else

Using configuration file in Outlook Addin
 
Joeri Torfs




Posts: 13
Joined: 2008-05-26
Hi,
I'm writing an outlook add-in which displays a new button in the 'New Message' form.
I'm using a ADX COM Add-in for this.
I have a question involving my add-in.

1) I need a configuration file (app.config) to provide me some settings. But I can't seem to figure out how to make my add-in work with an app.config.
Is there demo project available to provide me with some answers?

Sincerely,

Joeri
Posted 26 May, 2008 06:11:26 Top
Sergey Grischenko


Add-in Express team


Posts: 7230
Joined: 2004-07-05
Hi Joeri.

Here is the code that you can use to read your settings from the config file:

NameValueCollection appSettings =
ConfigurationManager.AppSettings;

IEnumerator appSettingsEnum =
appSettings.Keys.GetEnumerator();

int i = 0;
while (appSettingsEnum.MoveNext())
{
//string key = appSettings.Keys[i];
string value = appSettings[<a key>];

........

i += 1;
}

You also need to set the name of the config file in the adxloader.dll.manifest file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<assemblyIdentity name="<Add-in Assembly>, PublicKeyToken=<...>" />
<loaderSettings generateLogFile="true" shadowCopyEnabled="true" privileges="user" configFileName="app.config" />
</configuration>

Posted 27 May, 2008 10:19:11 Top
Joeri Torfs




Posts: 13
Joined: 2008-05-26
Hi,

Thanks for the reply. I've tried it but I'm still not able to read my appsettings in my App.config.

This is my adxloader.dll.manifest file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<assemblyIdentity "MyAddin1"/>
<loaderSettings generateLogFile="true" shadowCopyEnabled="true" privileges="user" configFileName="App.config" />
</configuration>

(The PublicKeyToken in the properties of my Primary Output is empty, so i didn't use this in the manifest file)

this is my App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Test" value="MyValue"/>
</appSettings>
</configuration>

and this is my code:


        private void adxOutlookEvents_ItemSend(object sender, AddinExpress.MSO.ADXOlItemSendEventArgs e)
        {
            NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
            IEnumerator appSettingsEnum = appSettings.Keys.GetEnumerator();
            int i = 0;
            while (appSettingsEnum.MoveNext())
            {
                string value = appSettings[i];
                i += 1;
            }
        }


He never enters the while statement and when i quickwatch, there are no keys to be found inside my appSettings.

I use Visual Studio 2008 under Windows XP.
I use a ADC COM-Addin.

Sincerely,

Joeri
Posted 28 May, 2008 05:45:40 Top
Sergey Grischenko


Add-in Express team


Posts: 7230
Joined: 2004-07-05
Hi Joeri.

Is the App.config file copied (while the add-in is built) in the output directory of the add-in project? Please check it.
Posted 28 May, 2008 07:59:25 Top
Joeri Torfs




Posts: 13
Joined: 2008-05-26
Hi Sergey,

Where do i find the output directory of the add-in project?
Is it the directory where my Add-In project builds to? Or is it the directory where my MyAddin1Setup.msi file is located?
Either way, the App.config file is not copied in any of them.
Is adding a post-build event to my Add-in project going to do the trick?
If so, to which of both directories (Add-in build directory or MyAddin1Setup.msi direcory) should it be copied to?

Sincerely,

Joeri
Posted 28 May, 2008 08:08:05 Top
Sergey Grischenko


Add-in Express team


Posts: 7230
Joined: 2004-07-05
You just need to open the properties of the App.config file in VS and set the 'Copy To Output Directory' property to 'Copy always'.
Posted 28 May, 2008 12:12:45 Top
Joeri Torfs




Posts: 13
Joined: 2008-05-26
Hi Sergey,

Thanks again for the quick reply.
I've tested your solution, but still my Add-in seems to find nothing.


        private void adxOutlookEvents_ItemSend(object sender, AddinExpress.MSO.ADXOlItemSendEventArgs e)
        {
            NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
            IEnumerator appSettingsEnum = appSettings.Keys.GetEnumerator();
            int i = 0;
            while (appSettingsEnum.MoveNext())
            {
                string value = appSettings[i];
                i += 1;
            }
        }


In my appSettings, the 'AllKeys' Property is set to string[0].
Is it possible to provide some sort of demo project for this matter?

Sincerely,

Joeri
Posted 29 May, 2008 04:27:37 Top
Sergey Grischenko


Add-in Express team


Posts: 7230
Joined: 2004-07-05
Hi Joeri.

Here is a working example:
http://www.add-in-express.com/projects/xllmoduleexample.zip
You will have to remove the Xll Additional module from the project because the example is written for the latest version of Add-in Express.
Posted 29 May, 2008 11:11:53 Top
Joeri Torfs




Posts: 13
Joined: 2008-05-26
Hi Sergey,

Thanks for the example.
Your example does find the app.config and does read it's content.
However, when i try to remake your example (either in excell using xll Add-in or outlook using COM-Addin) it fails... It reads no config settings. It really got me puzzled...
Is there any way I could send you my demo project to be reviewed?

Sincerely,

Joeri
Posted 30 May, 2008 04:06:07 Top
Sergey Grischenko


Add-in Express team


Posts: 7230
Joined: 2004-07-05
Hi Joeri.

Please use the VS debugger to determine why your code doesn't work.
Also you need to check the XML in your config file. Probably it contains some errors.
Posted 30 May, 2008 08:31:32 Top