1

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

System.Diagnostics.Process.Start(filePath);

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

在这种情况下,我想使用“打开方式”选项,在这里我可以从列表中选择一个程序,或者在网上搜索。如果我用鼠标打开文件的上下文菜单,这是可用的: 在此处输入图像描述

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

谢谢, 奥马尔

4

3 回答 3

2

在这里找到我的答案:

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

和这里:

检测在 c# 中使用 openas_rundll 打开的选定程序

合并的解决方案工作正常:

 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));
    }
    }
于 2013-06-18T12:40:24.627 回答
0

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

或者,您可以参考有关明确显示“打开方式”对话框的问题。

于 2013-06-18T11:58:11.160 回答
-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"_ 当提供文件名作为参数时,使用for 。verb
"\"%1\"arguments

于 2013-06-18T11:59:03.590 回答