tags:

views:

19

answers:

2

I have a WPF application that gets installed on the client machine through the Windows Installer. This application also registers a file extension (.xyz), so when the user double clicks a file, it opens my WPF application and displays the file (like Microsoft Word). This program also has a ton of files that are not marked as resource or content files that it uses (user manuals, part drawings, etc).

The problem comes when the user double clicks a .xyz file and it opens the WPF application. The application now has a working directory of the directory where the .xyz file is located. Now the program cannot find any of the files (user manuals, part drawings, etc) it needs.

What is the best way to handle this type of problem? I could set the working directory (Environment.CurrentDirectory), but my open file dialog box changes the working directory when the user saves or opens an .xyz file. I could use a pack uri for the part drawings, but I use Process.Start for the user manuals because they are a PDF. I tried searching, but couldn't come up with anyting.

+1  A: 

You should be able to get to your install directory either by finding the executable's directory or by using reflection to find an assemly's directory:

By finding executable, you could add a reference to Windows.Forms to make this work (admittedly not ideal):

using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

Using reflection:

using System.IO;
using System.Reflection;

string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).CodeBase);

Or

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

You can probably just cache that path onload of your app as it will not change.

brendan
Application.ExecutablePath does not exist in WPF, so I used reflection. This works for the PDF files and as for the Image files, I'm using a pack URI. It just seems like there would be some better way of handling this scenerio.
Wili
Added an alternative method...
brendan