【问题标题】:send file of unknown type to "open with" dialog将未知类型的文件发送到“打开方式”对话框
【发布时间】:2013-06-14 14:45:42
【问题描述】:

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

System.Diagnostics.Process.Start(filePath);

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

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

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

谢谢, 奥马尔

【问题讨论】:

    标签: c# .net windows file


    【解决方案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】:

    如果您的问题是注册文件类型(确切地说是文件扩展名),您可以使用它。
    我在我的应用程序安装程序中成功使用了它。

        /// <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

    【讨论】:

    • 谢谢,但这不是我想做的。我只想向用户显示打开方式对话框,以便他可以选择一个程序来打开文件。
    【解决方案3】:

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

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

    【讨论】:

    • 我会再次检查,但我很确定我遇到了异常。也许这个对话框显示为 FileDialog 的一部分?我正在使用 System.Diagnostics.Process.Start 打开文件,因为我不需要用户选择文件,我已经知道路径。谢谢
    • 您的链接很有帮助,仅在 Delphi 中。我在 C# 中找到了类似的解决方案。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    相关资源
    最近更新 更多