Is there any simple way to open the "Open with" file dialog?
-
4Winforms? WPF? Webforms? MVC? – Oded Jan 18 '11 at 16:31
-
CodeProject has an article which comes top when searching Google: http://www.codeproject.com/KB/shell/openwith.aspx. However, I'd like to see a prettier way of doing this, rather than resort to externs (`ProcessStartInfo`, perhaps?). – Grant Thomas Jan 18 '11 at 16:34
-
2http://stackoverflow.com/a/32153874/5306861 – codeDom Oct 23 '16 at 08:25
4 Answers
Some reverse-engineering with ProcExp revealed a rundll32.exe command line that worked. Here's a sample program that uses it:
using System;
using System.Diagnostics;
using System.IO;
class Program {
static void Main(string[] args) {
ShowOpenWithDialog(@"c:\temp\test.txt");
}
public static void ShowOpenWithDialog(string path) {
var args = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll");
args += ",OpenAs_RunDLL " + path;
Process.Start("rundll32.exe", args);
}
}
Tested on Win7, I cannot guess how well this will work on other versions of Windows.

- 922,412
- 146
- 1,693
- 2,536
-
Thank you @Hans! I was hitting around the bush for a while and this is what I needed. I wasn't using the full path of shell32.dll. – tehDorf Aug 12 '13 at 23:29
-
2Note that `OpenAs_RunDLL` is undocumented and [does not always work.](http://stackoverflow.com/questions/23566667/rundll32-shell32-dll) – Harry Johnston May 10 '14 at 01:43
-
I would replace Environment.GetFolderPath(Environment.SpecialFolder.System) with Environment.SystemDirectory. It is shorter and has the same result. – Alexandru Dicu Jan 21 '19 at 07:09
-
Old question (but still relevant!), but Harry Johnston's answer in his linked comment to use `SHOpenWithDialog` is the better answer I believe. With that said, I tried Han's answer on Win10 1909 and it worked fine with the handful of types I tested with. – Richard Moss Jan 04 '20 at 08:43
-
[What’s the guidance on when to use rundll32? Easy: Don’t use it](https://devblogs.microsoft.com/oldnewthing/20130104-00/?p=5643). – IInspectable Aug 18 '22 at 15:42
This ought to do the trick...
var processInfo = new ProcessStartInfo(fileName);
processInfo.Verb = "openas";
Process.Start(processInfo);
Although, Oded makes a great point - not knowing exactly how/where you intend to use such functionality means this might not be the answer for your situation.
Recent comments on this answer go to show I wasn't very detailed in the first place. A problem will arise if you try to openas
a file that already has the open
verb defined against that type of file. Similarly, if you try to open
a file that doesn't have that verb defined there'll be trouble. The issue would be:
Win32Exception: No application is associated with the specified file for this operation
Off the top of my head I suggested to Thomas that in order to use this kind of code in a production application you would need to be thorough and perhaps check the registry, or otherwise find out whether or not a file can and should be opened with any given verb. It could be simpler than that when you consider ProcessStartInfo.Verbs
: this will, once the fileName
is set, provide you with a collection of possible verbs associated with the file type. This should make it easier to determine what to do with which file.
To wrap up, as I mentioned to Thomas, you will need to take care and add some complexity/intelligence to your application - this answer certainly isn't a catch-all solution.

- 44,454
- 10
- 85
- 129
-
1Doesn't work... Win32Exception, "No application is associated with the specified file for this operation" – Thomas Levesque Apr 25 '12 at 12:45
-
@ThomasLevesque It will only work if no 'default programs' are defined; that is to say, if there is a `Open with...` context menu for the file then this will fail. If it's an unknown file type with simply an `Open` option then this will work. The reverse is also true: using the `open` verb for known file types will launch the default program but wil lraise this same exception if unknown. So, in a proper application you want to be thorough about this; maybe doing a lookup in the known file types repo (registry) and basing your decision of how to open on facts given by the system. – Grant Thomas Apr 25 '12 at 13:03
-
@ThomasLevesque In fact, you don't even need to go to such lengths: [`ProcessStartInfo.Verbs`](http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verbs(VS.90).aspx) will be populated with the possible verbs for a file when the `fileName` is set, meaning you can just check if the `open` verb is defined and if not use `openas`. This doesn't catch all cases I bet, so add your own cleverness at will. – Grant Thomas Apr 25 '12 at 13:12
-
Thanks @ThomasLevesque, this was very useful to me. With `ErrorDialog`, you can open **unassociated files** in file system as when you open it from a folder in windows explorer, showing the dialog to browse apps. Without this, my app was crashing when opening unassociated files. – jelies Jul 31 '12 at 11:43
-
-1 My understanding is this only works if the files doesn't have a default application, which is not any different than just calling Process.Start on the file name itself. Did I miss something? – tehDorf Aug 12 '13 at 23:26
Using
System.Diagnostics.Process.Start(path);
The file will be openened with the default program, if no default program is defined the open with dialog will be shown.
You can use the the function:
[DllImport("shell32.dll", SetLastError = true)]
extern public static bool
ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);
You have an example to use this function on: this link

- 2,188
- 1
- 18
- 21
There are tons of shell examples in the All In One Code Framework. Maybe you can take a look at them to see whether an example has functions you want to have.

- 39,551
- 56
- 175
- 291