2012-07-06 58 views
65

最簡單的方法來打開與默認應用程序文件是:C#與默認應用程序打開的文件和參數

System.Diagnostics.Process.Start(@"c:\myPDF.pdf"); 

不過,我想知道是否存在一種方法來設置參數的默認應用程序,因爲我想在確定的頁碼中打開pdf。

我知道我該怎麼做它創建一個新的過程並設置參數,但這樣我需要指出應用程序的路徑,我想有一個便攜式應用程序,而不是必須設置每次我在其他計算機上使用應用程序時,應用程序的路徑。我的想法是,我期望計算機安裝了pdf閱讀器,並只說明打開哪個頁面。

謝謝。

+0

您的意思是將參數發送給Adobe可執行文件而不是pdf文件,但不使用完整路徑? – 2012-07-06 16:19:52

+2

你對此有何看法?如果您不知道應用程序的路徑,則不知道哪個是默認的PDF查看器,而且您不知道使用哪種參數格式。 – ken2k 2012-07-06 16:20:48

+1

僅僅說'myProcess.StartInfo.FileName ='Acrobat.exe';'不給應用程序的完整路徑? – daniloquio 2012-07-06 16:23:34

回答

34

EDIT(感謝surfbutler評論中的問題評論) 如果你想用默認應用程序打開文件,我的意思是沒有指定Acrobat或Reader,你不能打開規範中的文件ified頁面。

在另一方面,如果你確定與指定Acrobat或Reader,請繼續閱讀:


你可以不用告訴完全版的Acrobat的路徑,像這樣:

Process myProcess = new Process();  
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path 
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" C:\\example.pdf"; 
myProcess.Start(); 

如果您不希望pdf使用Reader打開,但使用Acrobat打開第二行,如下所示:

myProcess.StartInfo.FileName = "Acrobat.exe"; 

第二個編輯:找到的PDF擴展

您可以查詢註冊表來確定默認的應用程序來打開PDF文件,然後相應地對過程的定義StartInfo的文件名默認的應用程序。Again, thanks surfbutler for your comment :)

按照做,對於細節這個問題:Finding the default application for opening a particular file type on Windows

+2

+1另外,我認爲可以查找與任何文件類型相關的應用程序,例如'.pdf'在註冊表中,然後將該名稱放在filename參數中。請參閱http://stackoverflow.com/questions/162331/finding-the-default-application-for-opening-a-particular-file-type-on-windows?rq=1 – Surfbutler 2012-07-06 16:38:27

+0

是否有一些打開的參數來獲取Adobe Reader在2頁視圖中打開?只是好奇,如果有人知道,那將是真正有用的。我一直在四處尋找,但我找不到任何東西。 – 2014-11-06 16:02:49

3

你可以用

Process process = new Process(); 
process.StartInfo.FileName = "yourProgram.exe"; 
process.StartInfo.Arguments = ..... //your parameters 
process.Start(); 
-4

嘗試請添加設置屬性下的項目,並利用他們的這種方式,你有乾淨的,易於配置的設置,可以配置成默認

How To: Create a New Setting at Design Time

更新:以下評論後

  1. 右鍵+點擊項目
  2. 添加新項
  3. 在Visual C#項目 - >常規
  4. 選擇設置文件
+9

錯誤的標籤也許? ;) – ken2k 2012-07-06 16:25:13

7

我轉換由xsl鏈接到C#博客文章的VB代碼,並修改了一點:

public static bool TryGetRegisteredApplication(
        string extension, out string registeredApp) 
{ 
    string extensionId = GetClassesRootKeyDefaultValue(extension); 
    if (extensionId == null) 
    { 
     registeredApp = null; 
     return false; 
    } 

    string openCommand = GetClassesRootKeyDefaultValue(
      Path.Combine(new[] {extensionId, "shell", "open", "command"})); 

    if (openCommand == null) 
    { 
     registeredApp = null; 
     return false; 
    } 

    registeredApp = openCommand 
        .Replace("%1", string.Empty) 
        .Replace("\"", string.Empty) 
        .Trim(); 
    return true; 
} 

private static string GetClassesRootKeyDefaultValue(string keyPath) 
{ 
    using (var key = Registry.ClassesRoot.OpenSubKey(keyPath)) 
    { 
     if (key == null) 
     { 
      return null; 
     } 

     var defaultValue = key.GetValue(null); 
     if (defaultValue == null) 
     { 
      return null; 
     } 

     return defaultValue.ToString(); 
    } 
} 

編輯 - 這是不可靠的。Finding the default application for opening a particular file type on Windows

相關問題