문제

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?

도움이 되었습니까?

해결책

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 :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top