我怎样才能知道C#应用程序是从桌面还是命令行启动的?
MSDN文档在这个部分有点模糊。也许有人可以帮我解决这个问题:)
非常感谢!
我怎样才能知道C#应用程序是从桌面还是命令行启动的?
MSDN文档在这个部分有点模糊。也许有人可以帮我解决这个问题:)
非常感谢!
这个似乎可以工作:
string[] args = System.Environment.GetCommandLineArgs();
if(args[0] == "you exe name"){ ...}
.GetCommandLineArgs()
,在典型的 static void Main(string[] args)
中,args []
参数已被删除。.exe
文件本身相同的路径运行。如果你从一个子文件夹 (foo\hello.exe
) 运行它,它将不起作用。 var pc = new PerformanceCounter("Process", "Creating Process Id",
Process.GetCurrentProcess().ProcessName);
var p = Process.GetProcessById((int)pc.RawValue);
不确定当有多个相同进程实例时是否有效。在这种情况下,最好使用这种方法:http://www.codeproject.com/KB/threads/ParentPID.aspx
我很确定没有一种方法可以知道这个,这就是为什么文档是“模糊的”的原因。
if (Console.Title == System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) {
Console.WriteLine("You clicked on this from Explorer, but this is a console app. Run it from a shell.");
Console.WriteLine("Press any key to continue (and have a great day)..."); Console.ReadKey(); return;
}
Console.WriteLine("Console app stuff MaxIsAwesome...");