Question

I have a WPF application that is run from a shortcut. Is there anyway I can access the directory location of the shortcut from within the WPF application?

Was it helpful?

Solution

From what I understand you cannot do that.

When you click on the shortcut, Windows reads the path to the target executable and execute it just like when you directly click the .exe file (with additional parameters specified in the shortcut of course). Your WPF application has no idea whether it is started by direct clicking, from the command line or from a shortcut; hence cannot get the folder in which the shortcut resides.

If your question refers to .exe location than use Reflection as in the other answer.

Hope this help :)

Edit:

A trick I use to get it is to set the "Start in" field in the Properties of the shortcut to be the folder containing the shortcut then get it by

Directory.GetCurrentDirectory();

though it is not reliable at all as many factors tend to change the current working folder value and you need to set this for every shortcut you use :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top