将未知类型的文件发送到“打开方式”对话框

时间:2013-06-18 11:45:01

标签: c# .net windows file

在我的应用程序中,我有一个保存的文件路径,我用它来尝试打开文件。 我正在使用这样的东西:

System.Diagnostics.Process.Start(filePath);

如果文件有关联的默认程序,这可以正常工作。 我的问题是关于文件没有关联的默认程序的情况。

在这种情况下,我想使用“打开方式”选项,我可以从列表中选择程序,或搜索网页。 如果我用鼠标打开文件的上下文菜单,这是可用的: enter image description here

是否可以通过编程方式执行此操作?

谢谢, 奥马尔

3 个答案:

答案 0 :(得分:2)

如果您使用无法识别扩展名的文件调用“开始”,您将获得标准的“Windows无法打开此文件”对话框。从那里,如果用户选择“从已安装程序列表中选择一个程序”,则他们会在菜单屏幕截图中看到与单击“选择默认程序...”时相同的对话框。

或者,您可以参考this question关于明确显示“打开方式”对话框。

答案 1 :(得分:1)

在这里找到我的答案:

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

在这里:

detect selected program opened using openas_rundll in c#

合并解决方案正常工作:

 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));
    }
    }

答案 2 :(得分:-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="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);

"open"使用verb 提供文件名作为参数时,请"\"%1\"使用arguments