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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
RunFileDlgAPI 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::FileRunmethod.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
RunFileDlgAPI 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.