我想要获得当前运行程序的名称,即程序的可执行名称。在C/ c++中,你从args[0]中得到它。


当前回答

更多选择:

System.Reflection.Assembly.GetExecutingAssembly () . getname () . name .CodeBase Path.GetFileName (System.Reflection.Assembly.GetExecutingAssembly () . getname ()

其他回答

这就足够了:

Environment.GetCommandLineArgs()[0];

更多选择:

System.Reflection.Assembly.GetExecutingAssembly () . getname () . name .CodeBase Path.GetFileName (System.Reflection.Assembly.GetExecutingAssembly () . getname ()

System.Diagnostics.Process.GetCurrentProcess()获取当前正在运行的进程。您可以使用ProcessName属性来确定名称。下面是一个示例控制台应用程序。

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Process.GetCurrentProcess().ProcessName);
        Console.ReadLine();
    }
}

如果你正在寻找可执行文件的完整路径信息,可靠的方法是使用以下方法:

   var executable = System.Diagnostics.Process.GetCurrentProcess().MainModule
                       .FileName.Replace(".vshost", "");

这消除了中间dll、vshost等的任何问题。

System.Reflection.Assembly.GetEntryAssembly()。如果程序集没有从内存中加载,Location返回exe名称的位置。 System.Reflection.Assembly.GetEntryAssembly()。CodeBase以URL的形式返回位置。