在c#中获取启动快捷方式
假设我有一个可执行文件,当它启动时我想知道它是如何启动的。即我想知道它是通过快捷方式启动还是直接启动。 这样:
string test = Environment.GetCommandLineArgs()[0];
我可以获得可执行文件的路径,但这始终是相同的,即使它是通过快捷方式启动的。
假设我的可执行文件名为 c:\text.exe,我直接启动它,然后 test = 'c:\test.exe' 如果我创建一个快捷方式,即 c:\shortcut.lnk (目标为 c:\test.exe),我希望 test 为“c:\shortcut.exe”,但它是“c:\test.exe”,
我强烈怀疑这一点这是不可能的,因为操作系统处理快捷方式部分,而可执行文件永远看不到差异,但也许有人有一个创造性的想法?
Lets say that I have an executable and when it is started I want to know how it's started. I.e. I would like to know if it is started with a shortcut or directly.
With this:
string test = Environment.GetCommandLineArgs()[0];
I can get the path of the executable, but this is always the same, even if it's started by a shortcut.
Lets say my executable is named c:\text.exe and I start it directly, then test = 'c:\test.exe'
If I create a shortcut i.e. c:\shortcut.lnk (with target c:\test.exe) I want test to be 'c:\shortcut.exe' but it is 'c:\test.exe'
I strongly suspect this to be impossible because the OS handles the shortcut part and the executable never can see the difference, but maybe someone has a creative idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这通常不起作用,但如果您要创建快捷方式,则可以添加命令行参数来识别它。
This won't work in general, but if you are creating the shortcut, you could add a command line parameter to identify it.
您的怀疑是正确的,操作系统向您隐藏了机制。
可以获取启动您的进程 , 然而。但这无法帮助您区分是从开始菜单中的链接启动还是从资源管理器窗口中双击启动。
Your suspicions are correct, with the operating system hiding the mechanics from you.
It is possible to get the process that started you, however. this won't help you differentiate whether you were started from a link in the start menu, vs. being doubled clicked on in the explorer window though.