omer schleifer
omer schleifer

Reputation: 3935

send file of unknown type to "open with" dialog

In my application I have a saved file path which I use to try and open the file. I am using something like this:

System.Diagnostics.Process.Start(filePath);

If the file has an associated default program this works fine. My question is regarding the scenario where the file does not have an associated default program.

In that case, I want to use the "open with" option , where I can choose a program from a list, or search the web. This is available if I open the context menu of the file with the mouse: enter image description here

Is it possible to do this programmatically?

Thanks, Omer

Upvotes: 1

Views: 1566

Answers (3)

omer schleifer
omer schleifer

Reputation: 3935

Found my answer here:

http://bytes.com/topic/c-sharp/answers/657117-opening-file-unknown-extension

and here:

detect selected program opened using openas_rundll in c#

Merged solution works fine:

 try
    {
  string path = @"c:\install.res.1028.dll";
    ProcessStartInfo pInfo = new ProcessStartInfo(path);
    Process.Start(pInfo );
    }
    catch (Win32Exception ex)
    {
    if (ex.ErrorCode == -2147467259)
    //ErrorCode for No application is associated with
    the specified file for
    //this operation
    {

            Process.Start("rundll32.exe", string.Format("shell32.dll,OpenAs_RunDLL \"{0}\"", path));
    }
    }

Upvotes: 2

joe
joe

Reputation: 8634

If your question is to register a file type (the file extension to be exact), you may use this.
I use it successfully in my application installer.

    /// <summary>
    /// Registers a file type. This enables users to open a file via double-clicking in the windows explorer. 
    /// ATTENTION: Providing a wrong extension or fileType will certainly cause a malfunction of other file types!!!
    /// </summary>
    /// <param name="extension"> The file extension (this may contain a starting '.') </param>
    /// <param name="fileType"> Name of the file type. </param>
    /// <param name="fileDescription"> Descriptive text for the file type. Displayed in the windows explorer. </param>
    /// <param name="verb"> Verb to use, usually 'open' </param>
    /// <param name="commandLine"> Commandline for the 'open' verb. Example: "C:\Zeiss\CmmCtrl\bin\ScriptTool.exe %1". You may use quotes ("") if any argument contains whitespaces. Don't forget to use a %1, which is replaced  </param>
    public static void RegisterFileType(string extension, string fileType, string fileDescription, string verb, string commandLine) {
        RegisterFileType(extension, fileType, fileDescription, verb, commandLine, "", -1);
    }


    /// <summary>
    /// Registers a file type. This enables users to open a file via double-clicking in the windows explorer. 
    /// ATTENTION: Providing a wrong extension or fileType will certainly cause a malfunction of other file types!!!
    /// </summary>
    /// <param name="extension"> The file extension (this may contain a starting '.') </param>
    /// <param name="fileType"> Name of the file type. </param>
    /// <param name="fileDescription"> Descriptive text for the file type. Displayed in the windows explorer. </param>
    /// <param name="verb"> Verb to use, usually 'open' </param>
    /// <param name="programPath"> Full path to the program to open the file. You may use quotes ("") if any argument contains whitespaces. Example: "C:\Zeiss\CmmCtrl\bin\ScriptTool.exe" </param>
    /// <param name="arguments"> Commandline for the 'open' verb. Don't forget to use a "%1". You may use quotes ("") if any argument contains whitespaces. Example: "%1"</param>
    /// <param name="iconIndex"> Index of the icon within the executable. Use -1 if not used. </param>
    public static void RegisterFileType(string extension, string fileType, string fileDescription, string verb, string programPath, string arguments, int iconIndex) {
        if (!extension.StartsWith(".")) extension = "." + extension;

        RegistryKey key;
        string commandLine = programPath + " " + arguments;

        // 1) Extension
        //    HKEY_CLASSES_ROOT\.zzz=zzzFile
        key = Registry.ClassesRoot.CreateSubKey(extension);
        key.SetValue("", fileType);
        key.Flush();

        // 2) File type
        //    HKEY_CLASSES_ROOT\zzzFile=ZZZ File
        key = Registry.ClassesRoot.CreateSubKey(fileType);
        key.SetValue("", fileDescription);
        key.Flush();

        // 3) Action (verb)
        //    HKEY_CLASSES_ROOT\zzzFile\shell\open\command=Notepad.exe %1
        //    HKEY_CLASSES_ROOT\zzzFile\shell\print\command=Notepad.exe /P %1
        key = Registry.ClassesRoot.CreateSubKey(fileType + "\\shell\\" + verb + "\\command");
        key.SetValue("", commandLine);
        key.Flush();

        // 4) Icon
        if (iconIndex >= 0) {
            key = Registry.ClassesRoot.CreateSubKey(fileType + "\\DefaultIcon");
            key.SetValue("", programPath + "," + iconIndex.ToString());
            key.Flush();
        }

        // 5) Notify running applications
        SHChangeNotify(SHChangeNotifyEventID.SHCNE_ASSOCCHANGED, SHChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
    }

    /// <summary> Notifies the system of an event that an application has performed. An application should use this function if it performs an action that may affect the Shell. </summary>
    [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern void SHChangeNotify(SHChangeNotifyEventID wEventId, SHChangeNotifyFlags uFlags, IntPtr dwItem1, IntPtr dwItem2);

Use "open" for verb.
When providing a file name as argument, use "\"%1\" for arguments.

Upvotes: -1

Dan Puzey
Dan Puzey

Reputation: 34200

If you call Start with a file that has unrecognised extension, you'll get the standard "Windows can't open this file" dialogue. From there, if the user elects to "Select a program from the list of installed programs," they then see the same dialogue as when you click "Choose default program..." in your menu screenshot.

Alternatively, you can refer to this question regarding explicitly displaying the "Open with" dialogue.

Upvotes: 0

Related Questions