gpt4 book ai didi

c# - 如何确定 C# 应用程序是从桌面还是从 cmd shell 启动的?

转载 作者:太空狗 更新时间:2023-10-29 22:35:17 25 4
gpt4 key购买 nike

我如何知道 C# 应用程序是否已从桌面或 cmd shell 启动?

msdn 文档在这部分有点模糊。也许有人可以在这里帮助我 :)

非常感谢!

最佳答案

这似乎可行:

string[] args = System.Environment.GetCommandLineArgs();

if(args[0] == "you exe name"){ ...}

如果双击它,args[0] 包含完整文件夹。

请注意,您需要实际调用 .GetCommandLineArgs(),即典型 static void Main(string[] 中的 args[] 参数args) 删除了此项。

-- 编辑

这只会检测它是否从与 .exe 本身相同的路径运行。如果您从子文件夹 (foo\hello.exe) 运行它,它将无法运行。

关于c# - 如何确定 C# 应用程序是从桌面还是从 cmd shell 启动的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com