2010-11-20 37 views
4

我的應用程序僅適用於我和同事,所以我不在乎它是Click-Once還是Copy-the-exe。我希望能夠在Windows資源管理器中單擊具有給定擴展名的文件,並啓動我的程序並打開該文件。我無法獲取它來捕獲文件名。WPF;點擊一次;雙擊文件啓動; VS 2008

表面上的解決方案:

http://blogs.msdn.com/b/avip/archive/2008/10/27/wpf-supporting-command-line-arguments-and-file-extensions.aspx

我想要的代碼如下,並在這一點上所有我想要做的就是把點擊文件名的文本框中。我懷疑我的相關無知是關於如何從Windows資源管理器引用一次性應用程序。當我建立時,我最終得到一個名爲setup.exe的文件,一個名爲Wis.application的文件,當我點擊「setup」安裝它時,我最終得到了一個「Click-once application reference」類型的快捷方式,在「 C:\ Users \ ptom \ AppData \ Roaming \ Microsoft \ Windows \ Start Menu \ Programs \ Wis「。我試着將文件與通過安裝創建的快捷方式相關聯,並將文件與setup.exe關聯。當我點擊該文件時,應用程序將啓動,但表示 AppDomain.CurrentDomain.SetupInformation.ActivationArguments爲空。 (通過「指示」我的意思是文本框被填充來自我測試的文本以查看它是否爲空)。如果我從調試中運行應用程序,或者只是通過從開始菜單運行應用程序,它會按照我所期望的那樣進行,遵循指示ActivationArguments不爲空的代碼路徑,但其ActivationData(string [])的長度0

下面是從app.xaml.cs代碼

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Windows; 

namespace Wis 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
     protected override void OnStartup(StartupEventArgs e) 
     { 

      // Check if this was launched by double-clicking a doc. If so, use that as the 

      // startup file name. 
      if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments == null) 
      { 
       this.Properties["DoubleClickedFileToLoad"] = "There were no activation arguments AGAIN"; 
      } 
      else 
      { 
       if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null 
            && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0) 
       { 
        this.Properties["DoubleClickedFileToLoad"] = 
        AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]; 
       } 
       else 
       { 
        this.Properties["DoubleClickedFileToLoad"] = "Type in a file name"; 
       } 
      } 


     } 
    } 
} 

感謝

回答

3

首先,你應該添加文件assoc命令爲ClickOnce,發佈(選擇項目 - >屬性 - >發佈 - 選項 - >文件關聯)
然後添加引用「System.Deployment
,那麼你可以提取App.cs路徑以這樣的方式,根據類型啓動(ClickOnce的或本地)

protected override void OnStartup(System.Windows.StartupEventArgs e) 
    { 
     var path = GetPath (e); 
    }   

    private static string GetPath(StartupEventArgs e) 
    { 
     if (!ApplicationDeployment.IsNetworkDeployed) 
      return e.Args.Length != 0 ? e.Args[0] : null; 
     if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments == null) 
      return null; 
     var args = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData; 
     return args == null || args.Length == 0 ? null : new Uri(args[0]).LocalPath; 
    } 
+0

我粘貼了你的代碼;你有一個名爲ApplicationDeployment的對象;我一開始就把它切掉了,它工作。我必須添加一個對System.Deployment的引用來使用第一個測試。 – 2010-11-20 20:37:02

3

你檢查Environment.GetCommandLineArgs()? 顯示您的應用程序啓動的參數。

如果您的應用程序與文件相關聯,它應該包含作爲參數之一的文件名。

+0

我讀了一對夫婦的地方,像這樣簡單的解決方案不工作,但現在我只是想它,它確實有效。所以我不知道該怎麼做。我將.../windows /開始菜單/程序/ Pwis文件夾中的點擊一次快捷方式與一個擴展名相關聯,當然,第二個參數是當我點擊具有給定擴展名的文件夾時的文件。所以這是比最小的答案更簡單的方法,不確定其他答案是否涵蓋更多方案? – 2010-11-20 22:25:26