
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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.
- Related Articles
- How to get the table name of the current ResultSet using JDBC?
- How to get current thread name in android?
- How to get current activity name in android?
- How to get current Bluetooth name in android?
- How to get current function name in PHP?
- How to display the name of the Current Thread in C#?
- How to get current country name from Network provider in android?
- How to get the current year in JavaScript?
- How to get the current date in Java?
- How to get the first day of the current month in MySQL?
- How to use the gecko executable with Selenium?
- How to get last day of the current month in MySQL?
- How to use the name of the current database to delete it in MySQL?
- How to Get the current language in Android device?
- How to get the only current year in JavaScript?
