24

Is there any simple way to open the "Open with" file dialog?

3
  • 4
    Winforms? WPF? Webforms? MVC? Commented Jan 18, 2011 at 16:31
  • CodeProject has an article which comes top when searching Google: 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?). Commented Jan 18, 2011 at 16:34
  • 2
    stackoverflow.com/a/32153874/5306861 Commented Oct 23, 2016 at 8:25

4 Answers 4

30

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
Note that OpenAs_RunDLL is undocumented and does not always work.
I would replace Environment.GetFolderPath(Environment.SpecialFolder.System) with Environment.SystemDirectory. It is shorter and has the same result.
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.
13

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.

5 Comments

Doesn't work... Win32Exception, "No application is associated with the specified file for this operation"
@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.
@ThomasLevesque In fact, you don't even need to go to such lengths: ProcessStartInfo.Verbs 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.
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.
-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?
1

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

Comments

-3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.