How can I find the location of my application's executable in WPF (C# or VB.Net)?
I've used this code with windows forms:
Application.ExecutablePath.ToString();
But with WPF I received this error from Visual Studio:
System.Window.Application does not contain a definition for ExecutablePath.
System.Reflection.Assembly.GetExecutingAssembly().Location
should work.
Several alternatives:
Directory.GetParent(Assembly.GetExecutingAssembly().Location)
System.AppDomain.CurrentDomain.BaseDirectory
Only in VB:
My.Application.Info.DirectoryPath
this is useful for you: Application.ExecutablePath equals to:
Process.GetCurrentProcess().MainModule.FileName;
The executing assembly can be a DLL if the code is located in a library:
var executingAssembly = Assembly.GetExecutingAssembly(); //MyLibrary.dll
var callingAssembly = Assembly.GetCallingAssembly(); //MyLibrary.dll
var entryAssembly = Assembly.GetEntryAssembly(); //WpfApp.exe or MyLibrary.dll
So the best way I found is (C#) :
var wpfAssembly = (AppDomain.CurrentDomain
.GetAssemblies()
.Where(item => item.EntryPoint != null)
.Select(item =>
new {item, applicationType = item.GetType(item.GetName().Name + ".App", false)})
.Where(a => a.applicationType != null && typeof(System.Windows.Application)
.IsAssignableFrom(a.applicationType))
.Select(a => a.item))
.FirstOrDefault();
So in your case, you can find location of the assembly :
var location = wpfAssembly.Location;
The following is applicable in the recent versions of .NET Core:
System.Environment.ProcessPath
This returns the path of the executable that started the currently running process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With