- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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#.
Using System.AppDomain −
Application domain provides isolation between code running in different app domains. App Domain is a logical container for code and data just like process and has separate memory space and access to resources. App domain also serves as a boundary like process does to avoid any accidental or illegal attempts to access the data of an object in one running application from another.
System.AppDomain class provides us the ways to deal with application domain. It provides methods to create new application domain, unload domain from memory etc.
This method returns the filename with the extension (ex: Application.exe).
Example
using System; namespace DemoApplication{ public class Program{ public static void Main(){ string currentExecutable = System.AppDomain.CurrentDomain.FriendlyName; Console.WriteLine($"Current Executable Name: {currentExecutable}"); Console.ReadLine(); } } }
Output
The output of the above code is
Current Executable Name: MyConsoleApp.exe
Using System.Diagnostics.Process −
A process is an operating system concept and it is the smallest unit of isolation provided by Windows OS. When we run an application, Windows creates a process for the application with a specific process id and other attributes. Each process is allocated with necessary memory and set of resources.
Every Windows process contains at least one thread which takes care of the application execution. A Processes can have many threads and they speed up execution and give more responsiveness but a process that contain a single primary thread of execution is considered to be more thread-safe.
This method returns the filename without extension (ex: Application).
Example 1
using System; namespace DemoApplication{ public class Program{ public static void Main(){ string currentExecutable = System.Diagnostics.Process.GetCurrentProcess().ProcessName; Console.WriteLine($"Current Executable Name: {currentExecutable}"); Console.ReadLine(); } } }
Output
The output of the above code is
Current Executable Name: MyConsoleApp
Example 2
using System; namespace DemoApplication{ public class Program{ public static void Main(){ string currentExecutable = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; Console.WriteLine($"Current Executable Name: {currentExecutable}"); Console.ReadLine(); } } }
Output
The output of the above code is
Current Executable Name: C:\Users\UserName\source\repos\MyConsoleApp\MyConsoleApp\bin\Debug\MyCo nsoleApp.exe In the above example we could see that Process.GetCurrentProcess().MainModule.FileName returns the executable file along with the folder.