Find the location of my application's executable in WPF (C# or vb.net)?

C#Wpfvb.net

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

System.Reflection.Assembly.GetExecutingAssembly().Location should work.

Solution 2 - C#

Several alternatives:

Directory.GetParent(Assembly.GetExecutingAssembly().Location)

System.AppDomain.CurrentDomain.BaseDirectory

Only in VB:

My.Application.Info.DirectoryPath

Solution 3 - C#

this is useful for you: Application.ExecutablePath equals to:

Process.GetCurrentProcess().MainModule.FileName;

Solution 4 - C#

Environment.CurrentDirectory returns parent directory of exe file

Solution 5 - C#

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;

Solution 6 - C#

This is what I use. It works even in debugger.

using System.IO;
using System.Diagnostics;

public static string GetMyBinDirectory()
{
    return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
}

It uses some powerfull classes, like Process and ProcessModule

Solution 7 - C#

Based on others answers, here's an example that shows how to remove the executable name from the path and combine the result with some subfolder and filename:

at my updated version of Hotspotizer (http://github.com/birbilis/Hotspotizer), I've just added support for loading a Gesture Collection file at startup, if found at Library\Default.hsjson, by using the following code:

const string GESTURE_COLLECTION_LIBRARY_PATH = "Library"
const string DEFAULT_GESTURE_COLLECTION = "Default.hsjson"

//...

LoadGestureCollection(
  Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  GESTURE_COLLECTION_LIBRARY_PATH,
  DEFAULT_GESTURE_COLLECTION));

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahinView Question on Stackoverflow
Solution 1 - C#BlorgbeardView Answer on Stackoverflow
Solution 2 - C#Konrad RudolphView Answer on Stackoverflow
Solution 3 - C#dexiangView Answer on Stackoverflow
Solution 4 - C#mahdi bView Answer on Stackoverflow
Solution 5 - C#scrat789View Answer on Stackoverflow
Solution 6 - C#marsh-wiggleView Answer on Stackoverflow
Solution 7 - C#George BirbilisView Answer on Stackoverflow
Solution 8 - C#Salih KavafView Answer on Stackoverflow