11

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.

4
  • 5
    Is there a reason you want the "Run" box instead of starting the process directly through Process.Start? Commented Feb 17, 2012 at 15:39
  • 11
    I have a very strong suspicion this is a "Shoe or Bottle?" question. Commented Feb 17, 2012 at 15:43
  • 1
    I understand you want to fire up the "Run" command provided by the Windows Explorer. Can you explain why you want to do this? Commented Feb 17, 2012 at 15:49
  • 3
    @user1216595 and that reason is? Commented Feb 17, 2012 at 15:50

3 Answers 3

9

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++?

4
  • Note that technically this is undocumented
    – Justin
    Commented Feb 17, 2012 at 15:53
  • Is this a undocumented call? I could not find any refrence to this on the MSDN or on pinvoke.net Commented Feb 17, 2012 at 15:54
  • 2
    I have doubts #61 remains stable over future releases.
    – BlueM
    Commented Feb 17, 2012 at 15:55
  • 4
    Do note that RunFileDlg is an undocumented function, which means that Microsoft may change its behaviour or remove it entirely at any time. The supported way is to use IShellDispatch::FileRun(), which can be accessed in C# by adding a reference to the COM library "Microsoft Shell Controls and Automation" and running var shell = new Shell32.Shell(); shell.FileRun();.
    – Sven
    Commented Feb 17, 2012 at 16:01
8

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.

0
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.