2016-03-21 40 views
1

我試圖打開他們喜歡此相關應用的不同文件的過程:我怎樣才能用於打開C#文件

   ProcessPath = @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"; 
       ProcessStartInfo processStartInfo = new ProcessStartInfo(); 
       processStartInfo.FileName = ProcessPath; 
       processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(ProcessPath); 

       process = new Process(); 
       process.StartInfo = processStartInfo; 
       process.EnableRaisingEvents = true; 
       process.Exited += Process_Exited; 
       process.Start();  

請你能幫助我獲取表示處理對象的保持應用程序打開,因爲在調用process.Start()之後;流程中的大部分字段都會引發異常。

謝謝。

+0

這是一個過程,儘管它不一定就緒(或者已經不再需要)了 - 不能保證要求shell打開一個文件會導致一個新的進程;認爲所有的Excel文件都在一個實例中打開例如Excel)。你想做什麼? – Luaan

+0

主要以其默認應用程序打開圖像文件或PDF。所以我上面發佈的代碼將打開Windows照片查看器中的默認窗口圖像之一,但啓動它的過程將是無用的,據我所知。 – Timmoth

+0

這個過程在你看的時候可能沒有做任何事情,但是你正在做的是等同於「start my.jpg」,而不是用你的jpg打開一個應用程序來打開,所以process.WaitForInputIdle();不會幫助,因爲該過程可能已經終止,因爲該過程然後催生了實際的應用程序 – BugFinder

回答

0

我設法找到一個解決方案:使用Windows API函數

 [DllImport("shell32.dll")] 
    static extern int FindExecutable(string file, string directory, [Out] StringBuilder lpResult); 

返回關聯的應用程序路徑給定文件

。然後,我可以使用我發佈的原始代碼執行與原始文件傳遞相關的應用程序作爲參數!