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

时间:2021-03-02 16:01:26

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].

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

20 个解决方案

#1


System.AppDomain.CurrentDomain.FriendlyName

#2


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

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

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

System.Diagnostics.Process.GetCurrentProcess()。ProcessName - 返回没有扩展名的文件名(例如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.

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

#3


System.Diagnostics.Process.GetCurrentProcess() gets the currently running process. You can use the ProcessName property to figure out the name. Below is a sample console app.

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

using System;
using System.Diagnostics;

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

#4


This should suffice:

这应该足够了:

Environment.GetCommandLineArgs()[0];

#5


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.

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

#6


Try this:

System.Reflection.Assembly.GetExecutingAssembly()

This returns you a System.Reflection.Assembly instance that has all the data you could ever want to know about the current application. I think that the Location property might get what you are after specifically.

这将返回一个System.Reflection.Assembly实例,该实例包含您可能想要了解的有关当前应用程序的所有数据。我认为Location属性可能会在具体后得到你所拥有的。

#7


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

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

会给你你的应用程序的FileName像; “MyApplication.exe”

#8


Why nobody suggested this, its simple.

为什么没有人提出这个,简单。

Path.GetFileName(Application.ExecutablePath)

#9


Couple more options:

结合更多选择:

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

#10


  • System.Reflection.Assembly.GetEntryAssembly().Location returns location of exe name if assembly is not loaded from memory.
  • System.Reflection.Assembly.GetEntryAssembly()。如果未从内存加载程序集,则Location返回exe名称的位置。

  • System.Reflection.Assembly.GetEntryAssembly().CodeBase returns location as URL.
  • System.Reflection.Assembly.GetEntryAssembly()。CodeBase将位置返回为URL。

#11


When uncertain or in doubt, run in circles, scream and shout.

当不确定或有疑问时,在圈子里奔跑,尖叫和喊叫。

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");
    }
}

I can't claim to have tested each option, but it doesn't do anything stupid like returning the vhost during debugging sessions.

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

#12


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.

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

#13


IF you are looking for the full path information of your executable, the reliable way to do it is to use the following:

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

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

This eliminates any issues with intermediary dlls, vshost, etc.

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

#14


You can use Environment.GetCommandLineArgs() to obtain the arguments and Environment.CommandLine to obtain the actual command line as entered.

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

Also, you can use Assembly.GetEntryAssembly() or Process.GetCurrentProcess().

此外,您可以使用Assembly.GetEntryAssembly()或Process.GetCurrentProcess()。

However, when debugging, you should be careful as this final example may give your debugger's executable name (depending on how you attach the debugger) rather than your executable, as may the other examples.

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

#15


Is this what you want:

这是你想要的吗:

Assembly.GetExecutingAssembly ().Location

#16


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

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

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

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

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.

无论我是在VS中运行实际的可执行文件还是调试,这都适用于我。

Note that it returns the application name without the extension.

请注意,它返回没有扩展名的应用程序名称。

John

#17


Try Application.ExecutablePath

#18


Super easy, here:

超级简单,这里:

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

#19


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:

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

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

#20


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

如果您只需要没有extensio的应用程序名称,则此方法有效:

 Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);

#1


System.AppDomain.CurrentDomain.FriendlyName

#2


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

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

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

System.Diagnostics.Process.GetCurrentProcess()。ProcessName - 返回没有扩展名的文件名(例如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.

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

#3


System.Diagnostics.Process.GetCurrentProcess() gets the currently running process. You can use the ProcessName property to figure out the name. Below is a sample console app.

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

using System;
using System.Diagnostics;

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

#4


This should suffice:

这应该足够了:

Environment.GetCommandLineArgs()[0];

#5


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.

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

#6


Try this:

System.Reflection.Assembly.GetExecutingAssembly()

This returns you a System.Reflection.Assembly instance that has all the data you could ever want to know about the current application. I think that the Location property might get what you are after specifically.

这将返回一个System.Reflection.Assembly实例,该实例包含您可能想要了解的有关当前应用程序的所有数据。我认为Location属性可能会在具体后得到你所拥有的。

#7


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

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

会给你你的应用程序的FileName像; “MyApplication.exe”

#8


Why nobody suggested this, its simple.

为什么没有人提出这个,简单。

Path.GetFileName(Application.ExecutablePath)

#9


Couple more options:

结合更多选择:

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

#10


  • System.Reflection.Assembly.GetEntryAssembly().Location returns location of exe name if assembly is not loaded from memory.
  • System.Reflection.Assembly.GetEntryAssembly()。如果未从内存加载程序集,则Location返回exe名称的位置。

  • System.Reflection.Assembly.GetEntryAssembly().CodeBase returns location as URL.
  • System.Reflection.Assembly.GetEntryAssembly()。CodeBase将位置返回为URL。

#11


When uncertain or in doubt, run in circles, scream and shout.

当不确定或有疑问时,在圈子里奔跑,尖叫和喊叫。

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");
    }
}

I can't claim to have tested each option, but it doesn't do anything stupid like returning the vhost during debugging sessions.

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

#12


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.

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

#13


IF you are looking for the full path information of your executable, the reliable way to do it is to use the following:

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

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

This eliminates any issues with intermediary dlls, vshost, etc.

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

#14


You can use Environment.GetCommandLineArgs() to obtain the arguments and Environment.CommandLine to obtain the actual command line as entered.

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

Also, you can use Assembly.GetEntryAssembly() or Process.GetCurrentProcess().

此外,您可以使用Assembly.GetEntryAssembly()或Process.GetCurrentProcess()。

However, when debugging, you should be careful as this final example may give your debugger's executable name (depending on how you attach the debugger) rather than your executable, as may the other examples.

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

#15


Is this what you want:

这是你想要的吗:

Assembly.GetExecutingAssembly ().Location

#16


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

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

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

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

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.

无论我是在VS中运行实际的可执行文件还是调试,这都适用于我。

Note that it returns the application name without the extension.

请注意,它返回没有扩展名的应用程序名称。

John

#17


Try Application.ExecutablePath

#18


Super easy, here:

超级简单,这里:

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

#19


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:

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

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

#20


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

如果您只需要没有extensio的应用程序名称,则此方法有效:

 Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);