如何启动windows“运行"来自 C# 的对话框 [英] How to start windows "run" dialog from C#

查看:23
本文介绍了如何启动windows“运行"来自 C# 的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 C# 代码中从 Windows 启动运行对话框 (Windows+R).

I want to start the run dialog (Windows+R) from Windows within my C# code.

我认为这可以使用 explorer.exe 完成,但我不确定如何.

I assume this can be done using explorer.exe but I'm not sure how.

推荐答案

RunFileDlg API 不受支持,并且可能会被 Microsoft 从未来版本的 Windows 中删除(我将承认 MS 承诺向后兼容性以及该 API 虽然没有记录,但似乎广为人知的事实使得这不太可能,但仍有可能).

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).

支持的启动运行对话框的方式是使用 IShellDispatch::FileRun 方法.

The supported way to launch the run dialog is using the IShellDispatch::FileRun method.

在 C# 中,您可以通过转到添加引用、选择 COM 选项卡并选择Microsoft Shell 控件和自动化"来访问此方法.完成此操作后,您可以按如下方式启动对话框:

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:

Shell32.Shell shell = new Shell32.Shell();
shell.FileRun();

是的,RunFileDlg API 提供了更多的可定制性,但它的优点是可以记录和支持,因此将来不太可能中断.

Yes, the RunFileDlg API offers more customizability, but this has the advantage of being documented, supported, and therefore unlikely to break in the future.

请注意,Shell32 必须在 STA 线程上运行.如果代码中出现异常,请在方法声明上方添加 [STAThread],如下所示,例如:

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:

    [STAThread]
    private static void OpenRun() {
        //Shell32 code here
    }

任何调用使用 Shell32 的方法的方法也应该在 STA 线程上运行.

Any method calling a method that uses Shell32 should also be run on an STA thread.

这篇关于如何启动windows“运行"来自 C# 的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆