[英]How to detect the context from which an application is being executed? Whether from Command Prompt or from within a Windows Form
[英]detect whether a program is being run via command prompt
有没有办法检测 c# xamarin 控制台库是否正在运行或正在通过终端运行? 这样我就可以添加
console.read();
如果用户直接运行程序,那么他们可以看到程序的 output
这样的事情应该使您朝正确的方向前进(( https://msdn.microsoft.com/zh-cn/library/z3w4xdc9(v=vs.110).aspx )
var processes = Process.GetProcessesByName("Name");
if(processes.Count > 0)
{
//do something here
}
使用命令
Environment.CommandLine
允许打开程序的命令。 如果程序在命令行中运行,则Environment.CommandLine将显示
ProgramName -parameter
但是,如果通过资源管理器或调试程序调用它,则将其称为
"PathToFile/ProgramName.exe"
这使过程可以查看是通过资源管理器调用还是通过某些if语句调用CMD。
如果您从命令行运行,则MainWindowTitle
将为空。 因此,让我们首先检查我们是否正在运行UserInteractive
,然后检查标题是否为空白。
private bool GUIMode()
{
return Environment.UserInteractive && !string.IsNullOrEmpty(Process.GetCurrentProcess().MainWindowTitle);
}
或VB
Private Function GUIMode() As Boolean
Return Environment.UserInteractive AndAlso Not String.IsNullOrEmpty(Process.GetCurrentProcess().MainWindowTitle)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.