Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the name of the current executable in C#?
There are several ways to get the name of the current executable in C#. Each method provides different levels of detail about the executable file, from just the name to the full path.
Using System.AppDomain
The AppDomain.CurrentDomain.FriendlyName property provides the name of the current application domain, which typically corresponds to the executable filename including its extension.
Example
using System;
namespace DemoApplication {
public class Program {
public static void Main() {
string currentExecutable = System.AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine($"Current Executable Name: {currentExecutable}");
}
}
}
The output of the above code is −
Current Executable Name: MyConsoleApp.exe
Using System.Diagnostics.Process
The System.Diagnostics.Process class provides several ways to retrieve information about the current process, including the executable name and full path.
Getting Process Name Only
This method returns the filename without the extension −
using System;
using System.Diagnostics;
namespace DemoApplication {
public class Program {
public static void Main() {
string currentExecutable = Process.GetCurrentProcess().ProcessName;
Console.WriteLine($"Current Executable Name: {currentExecutable}");
}
}
}
The output of the above code is −
Current Executable Name: MyConsoleApp
Getting Full Path with MainModule
This method returns the complete path to the executable file −
using System;
using System.Diagnostics;
namespace DemoApplication {
public class Program {
public static void Main() {
string currentExecutable = Process.GetCurrentProcess().MainModule.FileName;
Console.WriteLine($"Current Executable Path: {currentExecutable}");
}
}
}
The output of the above code is −
Current Executable Path: C:\Users\UserName\source\repos\MyConsoleApp\bin\Debug\MyConsoleApp.exe
Using System.Reflection.Assembly
The Assembly class provides another approach to get the executable location −
using System;
using System.IO;
using System.Reflection;
namespace DemoApplication {
public class Program {
public static void Main() {
string assemblyPath = Assembly.GetExecutingAssembly().Location;
string executableName = Path.GetFileName(assemblyPath);
Console.WriteLine($"Executable Name: {executableName}");
Console.WriteLine($"Full Path: {assemblyPath}");
}
}
}
The output of the above code is −
Executable Name: MyConsoleApp.exe Full Path: C:\Users\UserName\source\repos\MyConsoleApp\bin\Debug\MyConsoleApp.exe
Comparison of Methods
| Method | Returns | Use Case |
|---|---|---|
| AppDomain.CurrentDomain.FriendlyName | Filename with extension | Simple executable name |
| Process.GetCurrentProcess().ProcessName | Filename without extension | Process identification |
| Process.GetCurrentProcess().MainModule.FileName | Full path to executable | Complete file location |
| Assembly.GetExecutingAssembly().Location | Full path to assembly | Assembly-based applications |
Conclusion
Getting the current executable name in C# can be accomplished through multiple approaches depending on your needs. Use AppDomain.FriendlyName for simple name retrieval, Process.ProcessName for process-related operations, or MainModule.FileName when you need the complete file path.
