How to determine the folder where the WPF application (xbap) was launched?

I have been doing this for several hours. I found many links, including several here, to SO, which claim to show how to find the path to the launch, or the application directory. All proposed solutions return the location:

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

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

I am deploying this on the internal ABCDEF server, so to run this application I enter:

 \\ABCDEF\myApp.xbap 

I want to programmatically determine the server and folder. My reason is that every time you publish WPF with " automatically increment revision with each publish " enabled. In the folder where additional DLLs are located, there are additional programs on which this program depends.

I want to be able to dynamically determine the correct folder to view.

I 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 

None of this worked.

These links suggested many of the methods I tried:

Link1

Link2

Link3

Link4

Link5

+4
source share
2 answers

I think this will direct you in the right direction. Since this is like deploying through ClickOnce, you should get the necessary information here:

 System.Deployment.Application.ApplicationDeployment.CurrentDeployment 

this class can be found in System.Deployment.dll

Unfortunately, the CurrentDeployment object does not actually tell you where the application works, so you need to do one more job CurrentDeployment

if you call this:

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

you will get something like this (for data directory)

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 works here (and 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

  • Accepts the first file path
  • Defines a randomly generated folder name
  • finds an application folder based on the name of this folder (which contains your executable files)

I think that creating an extension method of type ApplicationDeployment would be appropriate.

Hope that helps

+4
source

This worked for me:

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

0
source

All Articles