bsh152s
bsh152s

Reputation: 3218

How do I run WPF application from different directory?

I've created a basic tool using WPF. If I run the tool from within the directory that it is located, everything works fine. But, if I run from a different directory, nothing happens. From the command line, it just returns to the prompt. I haven't had this problem with WinForms applications and the directory does include the one dll the tool depends on. What else could be going on?

EDIT: I think I figured out this problem. It was dying in InitializeComponent call within MainWindow.xaml.cs constructor. A property being bound to was attempting to access an object that was null. However, I have yet to figure out why the property is null only when I run from a different directory. Consider this problem solved.

Upvotes: 1

Views: 187

Answers (1)

Jesse Chisholm
Jesse Chisholm

Reputation: 4026

re: why the property is null

I suspect that InitializeComponent is trying to load some resources and is looking in the WorkingDirectory for the resources file.

Try this:

var realWD = Environment.CurrentDirectory;
Environment.CurrentDirectory =
        System.IO.Path.GetDirectory(
            System.Reflection.Assembly.GetEntryAssembly().Location);
InitializeComponent();
Environment.CurrentDirectory = realWD;

Edit: just reformatted to avoid horizontal scrolling.

-Jesse

Upvotes: 3

Related Questions