How do I get the name of the current executable in

2019-01-04 18:32发布

I want to get the name of the currently running program, that is the executable name of the program. In C/C++ you get it from args[0].

20条回答
SAY GOODBYE
2楼-- · 2019-01-04 18:57

System.AppDomain.CurrentDomain.FriendlyName - Returns the filename with extension (e.g. MyApp.exe).

System.Diagnostics.Process.GetCurrentProcess().ProcessName - Returns the filename without extension (e.g. MyApp).

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName - Returns the full path and filename (e.g. C:\Examples\Processes\MyApp.exe). You could then pass this into System.IO.Path.GetFileName() or System.IO.Path.GetFileNameWithoutExtension() to achieve the same results as the above.

查看更多
成全新的幸福
3楼-- · 2019-01-04 18:57

Couple more options:

  • System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
  • Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
查看更多
狗以群分
4楼-- · 2019-01-04 18:58

Why nobody suggested this, its simple.

Path.GetFileName(Application.ExecutablePath)
查看更多
走好不送
5楼-- · 2019-01-04 18:59

This is the code which worked for me:

string fullName = Assembly.GetEntryAssembly().Location;
string myName = Path.GetFileNameWithoutExtension(fullName);

All the examples above gave me the processName with vshost or the running dll name.

查看更多
【Aperson】
6楼-- · 2019-01-04 19:00

This works if you need only the Application name without extensio:

 Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-04 19:01

This should suffice:

Environment.GetCommandLineArgs()[0];
查看更多
登录 后发表回答