如何启动Windows“运行”来自 C# 的对话框
我想在我的 C# 代码中从 Windows 启动运行对话框 (Windows+R)。
我认为这可以使用 explorer.exe 来完成,但我不确定如何完成。
I want to start the run dialog (Windows+R) from Windows within my C# code.
I assume this can be done using explorer.exe but I'm not sure how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 RunFileDlg:
flags
的可能值:另请参阅如何以编程方式打开运行c++?
Use RunFileDlg:
Possible values for
flags
:See also How to programmatically open Run c++?
RunFileDlg API 不受支持,可能会被 Microsoft 从 Windows 的未来版本中删除(我承认 MS 对向后兼容性的承诺,并且该 API 虽然没有记录,但似乎已广为人知)这使得这种情况不太可能发生,但仍然有可能)。
启动运行对话框的支持方法是使用
IShellDispatch::FileRun
方法。在 C# 中,您可以通过转到“添加引用”,选择“COM”选项卡,然后选择“Microsoft Shell 控件和自动化”来访问此方法。完成此操作后,您可以按如下方式启动对话框:
是的,
RunFileDlg
API 提供了更多的可定制性,但这具有已记录、受支持的优点,因此将来不太可能出现问题。请注意,Shell32 必须在 STA 线程上运行。如果代码中出现异常,请在方法声明上方添加
[STAThread]
,如下所示:任何调用使用 Shell32 的方法的方法也应该在 STA 线程上运行。
The
RunFileDlg
API is unsupported and may be removed by Microsoft from future versions of Windows (I'll grant that MS's commitment to backwards compatibility and the fact that this API, though undocumented, appears to be fairly widely known makes this unlikely, but it's still a possibility).The supported way to launch the run dialog is using the
IShellDispatch::FileRun
method.In C#, you can access this method by going to Add Reference, select the COM tab, and select "Microsoft Shell Controls and Automation". After doing this you can launch the dialog as follows:
Yes, the
RunFileDlg
API offers more customizability, but this has the advantage of being documented, supported, and therefore unlikely to break in the future.Note that Shell32 must be run on an STA thread. If you get an exception in your code, add
[STAThread]
above your method declaration like this, for example:Any method calling a method that uses Shell32 should also be run on an STA thread.
另一种方法是模拟 Windows+R 组合键。
并致电:
Another method would be to emulate the Windows+R key combination.
and call: