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


当前回答

来获取路径和名称

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

其他回答

为什么没人建议这个,很简单。

Path.GetFileName(Application.ExecutablePath)

如果您需要程序名称来设置防火墙规则,请使用:

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

这将确保在VisualStudio中调试和在windows中直接运行应用程序时名称是正确的。

System.AppDomain.CurrentDomain.FriendlyName

这是你想要的吗?

Assembly.GetExecutingAssembly ().Location

如果您要在。net 6.0或更高版本中发布单文件应用程序,则可以使用Environment。ProcessPath