如何启动Windows“运行”来自 C# 的对话框

发布于 2025-01-07 08:59:31 字数 94 浏览 0 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2025-01-14 08:59:31

使用 RunFileDlg:

[DllImport("shell32.dll", EntryPoint = "#61", CharSet = CharSet.Unicode)]
public static extern int RunFileDlg(
    [In] IntPtr hWnd,
    [In] IntPtr icon,
    [In] string path,
    [In] string title,
    [In] string prompt,
    [In] uint flags);

private static void Main(string[] args)
{
    // You might also want to add title, window handle...etc.
    RunFileDlg(IntPtr.Zero, IntPtr.Zero, null, null, null, 0);
}

flags 的可能值:

RFF_NOBROWSE = 1; //Removes the browse button.
RFF_NODEFAULT = 2; // No default item selected.
RFF_CALCDIRECTORY = 4; // Calculates the working directory from the file name.
RFF_NOLABEL = 8; // Removes the edit box label.
RFF_NOSEPARATEMEM = 14; // Removes the Separate Memory Space check box (Windows NT only).

另请参阅如何以编程方式打开运行c++?

Use RunFileDlg:

[DllImport("shell32.dll", EntryPoint = "#61", CharSet = CharSet.Unicode)]
public static extern int RunFileDlg(
    [In] IntPtr hWnd,
    [In] IntPtr icon,
    [In] string path,
    [In] string title,
    [In] string prompt,
    [In] uint flags);

private static void Main(string[] args)
{
    // You might also want to add title, window handle...etc.
    RunFileDlg(IntPtr.Zero, IntPtr.Zero, null, null, null, 0);
}

Possible values for flags:

RFF_NOBROWSE = 1; //Removes the browse button.
RFF_NODEFAULT = 2; // No default item selected.
RFF_CALCDIRECTORY = 4; // Calculates the working directory from the file name.
RFF_NOLABEL = 8; // Removes the edit box label.
RFF_NOSEPARATEMEM = 14; // Removes the Separate Memory Space check box (Windows NT only).

See also How to programmatically open Run c++?

夜巴黎 2025-01-14 08:59:31

RunFileDlg API 不受支持,可能会被 Microsoft 从 Windows 的未来版本中删除(我承认 MS 对向后兼容性的承诺,并且该 API 虽然没有记录,但似乎已广为人知)这使得这种情况不太可能发生,但仍然有可能)。

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

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

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

是的,RunFileDlg API 提供了更多的可定制性,但这具有已记录、受支持的优点,因此将来不太可能出现问题。

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

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

任何调用使用 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:

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

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:

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

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

在风中等你 2025-01-14 08:59:31

另一种方法是模拟 Windows+R 组合键。

using System.Runtime.InteropServices;
using System.Windows.Forms;

static class KeyboardSend
{
    [DllImport("user32.dll")]
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    private const int KEYEVENTF_EXTENDEDKEY = 1;
    private const int KEYEVENTF_KEYUP = 2;

    public static void KeyDown(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
    }

    public static void KeyUp(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

并致电:

KeyboardSend.KeyDown(Keys.LWin);
KeyboardSend.KeyDown(Keys.R);
KeyboardSend.KeyUp(Keys.R);
KeyboardSend.KeyUp(Keys.LWin);

Another method would be to emulate the Windows+R key combination.

using System.Runtime.InteropServices;
using System.Windows.Forms;

static class KeyboardSend
{
    [DllImport("user32.dll")]
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    private const int KEYEVENTF_EXTENDEDKEY = 1;
    private const int KEYEVENTF_KEYUP = 2;

    public static void KeyDown(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
    }

    public static void KeyUp(Keys vKey)
    {
        keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

and call:

KeyboardSend.KeyDown(Keys.LWin);
KeyboardSend.KeyDown(Keys.R);
KeyboardSend.KeyUp(Keys.R);
KeyboardSend.KeyUp(Keys.LWin);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文