views:

21

answers:

1

Hi,

I'm getting a file not found exception when my application is called by Outlook. It's called when an email is saved to the computer the app is called and performs an action on the saved message.

My app uses a XML file to store configurable settings but this file can't be found when Outlook calls to executes the application. If I run the program manually then it works fine.

The interesting thing about the exception is this:

System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Common Files\System\MSMAPI\1033\settingsOpened.xml'.

Why does Outlook think that the file is here ? This isn't the path for the file but I'm sure is related to Outlook. The way I'm referencing the path in the code is just:

XmlDocument xDoc = new XmlDocument();
xDoc.Load("settingsOpened.xml");

With the file being in the same folder as the .exe. I don't want to hard code the full path in for the XML files either.

Any help would be greatly appreciated.

Thanks, Ross

A: 

From the code shown I would assume that it takes the current path as the location to look for the file. The current path is a bit unpredictable as certain operations effect its value and the value is persisted on each call. I.e. when another part of your application, or even another applciation, sets the current path this value is used the next time around. To set the current path it is enought to use a common dialog to browse for a certain file.

In your case I'd try to either to

  • specify the path explicitly by deriving it from one of the well known folders (e.g. the user's app path - look for Environment.GetFolderPath and Environment.SpecialFolder)

or

  • to resolve the path relative to your Dll's assembly path.

To find the assembly path for myType you can use the following code:

String strPath = System.IO.Path.GetDirectoryName(typeof(myType).Assembly.CodeBase);

In either case you should consider that in newer windows operating systems the user does not have write access to all paths of the system drive.

Obalix