Michael Eakins
Michael Eakins

Reputation: 4179

How do I determine the folder my WPF(xbap) application was started from?

I have been investigating this for several hours. I have found numerous links, including several here on SO which claim to show how to find the startup path, or the application directory. All of the solutions suggested return a location:

C:\Users\<my user name>\AppData\Local\Apps\2.0\XO8PWL8B.5HH\1GZX7M0H.N1J\<temp location>\

When my WPF xbap is run from a remote location. I need to determine the actual folder of the remote location.

I am deploying this to an internal server ABCDEF, so in order to run this application I am entering:

\\ABCDEF\myApp.xbap

I want to programmatically determine the server and folder. My reason for this is that each time you publish a WPF with "automatically increment revision with each publish" turned on. The folder where additional DLL's are located changes, additional programs that this program depends on.

I want to be able to dynamically determine the correct folder to look at.

I have tried:

'Dim path As String = Reflection.Assembly.GetEntryAssembly().Location
'Dim Path As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
'System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly.Location)
'application.StartupPath

All of which did not work.

These links suggested many of the methods I have tried:

Link1

Link2

Link3

Link4

Link5

Upvotes: 4

Views: 3798

Answers (2)

Rumata
Rumata

Reputation: 91

This worked for me:

string exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName; string dir = Path.GetDirectoryName(exePath);

Upvotes: 0

Jose
Jose

Reputation: 11091

I think this will steer you in the right direction. Since it sounds like you're deploying via ClickOnce you should get the information you need here:

System.Deployment.Application.ApplicationDeployment.CurrentDeployment

this class can be found in the System.Deployment.dll

Unfortunately the CurrentDeployment object doesn't actually tell you where the application lives so you have to do some more work :(

if you call this:

var datadir = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory

you'll get something like this(for the datadirectory)

C:\Users\Administrator\AppData\Local\Apps\2.0\Data\BVPTZA5G.3AC\WC2WBZ92.D96\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341\Data\1.0.0.0

from there you need to get the folder name of the bold folder above because the application lives here(not the data directory):

C:\Users\Administrator\AppData\Local\Apps\2.0\RCVHD71Y.7CQ\BC42YMHT.ZQ0\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341

So I would create a function that

  • Takes in the first filepath
  • Determines the randomly generated foldername
  • finds the folder of the application based on that folder name(which contains your executables)

I think creating an extension method on type ApplicationDeployment would be fitting.

Hope that helps

Upvotes: 4

Related Questions