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条回答
对你真心纯属浪费
2楼-- · 2019-01-04 19:02
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;

will give you FileName of your app like; "MyApplication.exe"

查看更多
乱世女痞
3楼-- · 2019-01-04 19:02

If you need the Program name to set up a firewall rule, use:

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

This will ensure that the name is correct both when debugging in VisualStudio and when running the app directly in windows.

查看更多
爷的心禁止访问
4楼-- · 2019-01-04 19:02

For windows apps (forms and console) I use this:

Add a reference to System.Windows.Forms in VS then:

using System.Windows.Forms;
namespace whatever
{
    class Program
    {
        static string ApplicationName = Application.ProductName.ToString();
        static void Main(string[] args)
        {
            ........
        }
    }
}

This works correctly for me whether I am running the actual executable or debugging within VS.

Note that it returns the application name without the extension.

John

查看更多
欢心
5楼-- · 2019-01-04 19:05

On .Net Core (or Mono), most of the answers won't apply when the binary defining the process is the runtime binary of Mono or .Net Core (dotnet) and not your actual application you're interested in. In that case, use this:

var myName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);
查看更多
放荡不羁爱自由
6楼-- · 2019-01-04 19:06

Is this what you want:

Assembly.GetExecutingAssembly ().Location
查看更多
迷人小祖宗
7楼-- · 2019-01-04 19:07
System.AppDomain.CurrentDomain.FriendlyName
查看更多
登录 后发表回答