如何在c#中获得当前可执行文件的名称?

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

322072 次浏览

试试这个:

System.Reflection.Assembly.GetExecutingAssembly()

这将返回一个System.Reflection.Assembly实例,其中包含您希望了解的有关当前应用程序的所有数据。我认为Location属性可能会得到你特别想要的东西。

这就足够了:

Environment.GetCommandLineArgs()[0];

这是你想要的吗?

Assembly.GetExecutingAssembly ().Location

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

using System;
using System.Diagnostics;


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

你可以使用Environment.GetCommandLineArgs()获取参数,使用Environment.CommandLine获取实际输入的命令行。

同样,你也可以使用Assembly.GetEntryAssembly()Process.GetCurrentProcess()

但是,在调试时,您应该小心,因为最后一个示例可能会给出调试器的可执行文件名称(取决于您如何附加调试器),而不是可执行文件,其他示例可能也是如此。

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

更多选择:

  • System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
  • Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

System.AppDomain.CurrentDomain.FriendlyName -返回带扩展名的文件名(例如MyApp.exe)。

System.Diagnostics.Process.GetCurrentProcess().ProcessName -返回文件名没有扩展名(例如MyApp)。

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -返回完整的路径和文件名(例如C:\Examples\Processes\MyApp.exe)。然后你可以将其传递给System.IO.Path.GetFileName()System.IO.Path.GetFileNameWithoutExtension(),以实现与上面相同的结果。

当你不确定或有疑问时,绕着圈子跑,尖叫和大喊。

class Ourself
{
public static string OurFileName() {
System.Reflection.Assembly _objParentAssembly;


if (System.Reflection.Assembly.GetEntryAssembly() == null)
_objParentAssembly = System.Reflection.Assembly.GetCallingAssembly();
else
_objParentAssembly = System.Reflection.Assembly.GetEntryAssembly();


if (_objParentAssembly.CodeBase.StartsWith("http://"))
throw new System.IO.IOException("Deployed from URL");


if (System.IO.File.Exists(_objParentAssembly.Location))
return _objParentAssembly.Location;
if (System.IO.File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName))
return System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName;
if (System.IO.File.Exists(System.Reflection.Assembly.GetExecutingAssembly().Location))
return System.Reflection.Assembly.GetExecutingAssembly().Location;


throw new System.IO.IOException("Assembly not found");
}
}

我不能声称已经测试了每个选项,但它不会做任何愚蠢的事情,比如在调试会话期间返回vhost。

这是对我有用的代码:

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

上面所有的例子都给了我带有vshost或运行dll名称的processName。

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

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

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

System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;

会给你应用的文件名,比如;“MyApplication.exe”

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

Path.GetFileName(Application.ExecutablePath)

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

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

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

超级简单,在这里:

Environment.CurrentDirectory + "\\" + Process.GetCurrentProcess().ProcessName

在。net Core(或Mono)上,当定义进程的二进制文件是Mono或。net Core (dotnet)的运行时二进制文件,而不是您感兴趣的实际应用程序时,大多数答案都不适用。在这种情况下,使用这个:

var myName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);

对于windows应用程序(窗体和控制台),我使用这个:

在VS中添加对System.Windows.Forms的引用,然后:

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

无论我是运行实际的可执行文件还是在VS中调试,这对我来说都是正确的。

注意,它返回的是应用程序名称,而不是扩展名。

约翰

如果你只需要应用程序名而不需要扩展名,这是有效的:

Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);

来获取路径和名称

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

如果你在。net 6.0或更高版本中发布一个单文件应用程序,你可以使用环境。ProcessPath