Is there any simple way to open the "Open with" file dia开发者_如何学编程log?
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.
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.
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
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.
精彩评论