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
Release: 2023-09-21 23:01:02
forward
1262 people have browsed it

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();
      }
   }
}
Copy after login

Output

The output result of the above code is

Current Executable Name: MyConsoleApp.exe
Copy after login

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();
      }
   }
}
Copy after login

Output

The output result of the above code is

Current Executable Name: MyConsoleApp
Copy after login

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();
      }
   }
}
Copy after login

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.
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template