如何确定控制台应用程序是如何启动的?
如何判断用户是否通过双击 EXE(或快捷方式)启动了我的控制台应用程序,或者他们是否已经打开了命令行窗口并在该会话中执行了我的控制台应用程序?
How can I tell whether the user launched my console application by double-clicking the EXE (or a shortcut), or whether they already had a command line window open and executed my console app within that session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将此静态字段粘贴到您的“
Program
”类中,以确保它在任何输出之前运行:这有点矫枉过正/偏执,但会从资源管理器启动,而不响应诸如
之类的事情cls&& app.exe
(通过检查完整路径)甚至cls && “f:\ull\path\to\app.exe”
(通过查看标题)。我从 这个问题的 win32 版本中得到了这个想法< /a>.
Stick this static field in your "
Program
" class to ensure it runs before any output:This is a little bit overkill/paranoid, but picks up being started from Explorer while not responding to things like
cls && app.exe
(by checking for the full path) or evencls && "f:\ull\path\to\app.exe"
(by looking at the title).I got the idea from the win32 version of this question.
您也许可以通过 P/Invoking Win32 GetStartupInfo() 函数。
You might be able to figure it out by P/Invoking to the Win32 GetStartupInfo() function.
你可以找出父进程是什么:
其中Parent()是一个扩展方法,例如:
You can find out what the parent process is:
Where Parent() is an extension method, e.g.: