2010-11-20 35 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