Joa*_*kim 338 c# command-line
我想获取当前正在运行的程序的名称,即程序的可执行文件名称.在C/C++中,你可以从中获得它args[0].
Ste*_*owe 390
System.AppDomain.CurrentDomain.FriendlyName
Run Code Online (Sandbox Code Playgroud)
Lee*_*som 219
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()获得与上述相同的结果.
Aar*_*els 105
System.Diagnostics.Process.GetCurrentProcess()获取当前正在运行的进程.您可以使用该ProcessName属性来确定名称.以下是示例控制台应用程序.
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().ProcessName);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*s B 97
这应该足够了:
Environment.GetCommandLineArgs()[0];
Run Code Online (Sandbox Code Playgroud)
Tal*_*gal 19
这是对我有用的代码:
string fullName = Assembly.GetEntryAssembly().Location;
string myName = Path.GetFileNameWithoutExtension(fullName);
Run Code Online (Sandbox Code Playgroud)
上面的所有示例都给了我带有vshost的processName或运行的dll名称.
And*_*are 17
试试这个:
System.Reflection.Assembly.GetExecutingAssembly()
Run Code Online (Sandbox Code Playgroud)
这将返回一个System.Reflection.Assembly实例,其中包含您可能想要了解的有关当前应用程序的所有数据.我认为该Location物业可能会在具体后得到你所拥有的.
Teo*_*ahi 11
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;
Run Code Online (Sandbox Code Playgroud)
会给你你的应用程序的FileName像; "MyApplication.exe"
xme*_*men 11
为什么没有人提出这个,简单.
Path.GetFileName(Application.ExecutablePath)
Run Code Online (Sandbox Code Playgroud)
Joh*_*hnB 10
结合更多选择:
System.Reflection.Assembly.GetExecutingAssembly().GetName().NamePath.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBaseSystem.Reflection.Assembly.GetEntryAssembly().Location 如果未从内存加载程序集,则返回exe名称的位置.System.Reflection.Assembly.GetEntryAssembly().CodeBase 将位置返回为URL.当不确定或有疑问时,在圈子里奔跑,尖叫和喊叫.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我不能声称已经测试了每个选项,但它没有做任何愚蠢的事情,比如在调试会话期间返回vhost.
小智 8
如果您需要程序名称来设置防火墙规则,请使用:
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
Run Code Online (Sandbox Code Playgroud)
这将确保在VisualStudio中调试和直接在Windows中运行应用程序时名称正确.