2013-06-18 41 views
0

在我的應用程序中,我有一個保存的文件路徑,用於嘗試打開文件。 我使用的是這樣的:將未知類型的文件發送到「打開」對話框

System.Diagnostics.Process.Start(filePath); 

如果文件有關聯的默認程序能正常工作。 我的問題是關於文件沒有關聯的默認程序的情況。

在這種情況下,我想使用「打開方式」選項,我可以從列表中選擇一個程序,或搜索網頁。 這是可用的,如果我用鼠標打開文件的上下文菜單: enter image description here

是否有可能以編程方式做到這一點?

感謝, 奧馬爾

回答

2

如果調用開始與具有無法識別的擴展名的文件,你會得到標準的「Windows無法打開此文件」的對話。從那裏,如果用戶選擇「從已安裝程序列表中選擇一個程序」,則他們會看到與您在菜單屏幕截圖中單擊「選擇默認程序...」時相同的對話框。

或者,您可以參考this question關於明確顯示「打開方式」對話框。

+0

我會再次檢查,但我很確定我有一個例外。也許這個對話框顯示爲FileDialog的一部分?我打開System.Diagnostics.Process.Start文件,因爲我不需要用戶輸入文件,我已經知道路徑。謝謝 –

+0

只有在德爾福,您的鏈接纔有幫助。我在C#中找到了類似的解決方案。謝謝你的幫助。 –

0

如果你的問題是註冊一個文件類型(文件擴展名是確切的),你可以使用它。
我在我的應用程序安裝程序中成功使用它。

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

+0

謝謝,但這不是我想要做的。我只是想顯示打開的對話框給用戶,所以他可以選擇一個程序來打開文件。 –

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

請注意'OpenAs_RunDLL'沒有記錄,並且[並不總是有效。](http://stackoverflow.com/questions/23566667/rundll32-shell32-dl​​l) –