Home >Backend Development >C#.Net Tutorial >How to get the name of the current executable file in C#?

How to get the name of the current executable file in C#?

WBOY
WBOYforward
2023-09-21 23:01:021248browse

C# 如何获取当前可执行文件的名称?

There are several ways to get the name of the current executable file in C#.

Using System.AppDomain -

Application domains provide isolation between code running in different application domains.

area. An application domain is a logical container for code and data, just like processes and Has independent memory space and resource access. The application domain also acts as Boundary-like processes do prevent any accidental or illegal attempts to access In a running application, get the data of an object from another application.

The System.AppDomain class provides us with methods for handling application domains Provides methods to create new application domains and unload domains from memory etc

This method returns the file name with extension (for example: Application.exe).

Example

Real-time demonstration

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 result of the above code is

Current Executable Name: MyConsoleApp.exe

Use System.Diagnostics. Process -

Process is an operating system concept, it is the smallest isolation unit Provided by Windows operating system. When we run an application, Windows creates a process For applications with specific process IDs and other properties. Every process is Necessary memory and resources are allocated.

Each Windows process contains at least one thread responsible for processing application execution. A process can have multiple threads and they can speed things up performs and provides greater responsiveness, but a process that contains a single main process Threads of execution are considered more thread-safe.

This method returns the file name without extension (for example: Application).

Example 1

Real-time demonstration

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 result of the above code is

Current Executable Name: MyConsoleApp

Example 2

Demonstration

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 result 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.

The above is the detailed content of How to get the name of the current executable file in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete