WPF;单击一次;双击启动文件; VS 2008 [英] WPF; click once; Double Click file to launch; VS 2008

查看:207
本文介绍了WPF;单击一次;双击启动文件; VS 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序仅适用于我和同事,因此我不在乎单击一次还是复制该文件。我希望能够在Windows资源管理器中单击具有给定扩展名的文件,并启动我的程序并打开该文件。我无法获取文件名。

My application is only for me and co-workers, so I don't care if it's Click-Once or copy-the-exe. I want to be able to click a file with given extension in windows explorer and have my program launch and open that file. I can't get it to capture the file name.

可行的解决方案:

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

我要尝试的代码在下面,这时我要做的就是将被单击文件的名称放在文本框中。我怀疑我的相关无知是关于如何引用Windows资源管理器中的单击一次应用程序。构建后,我得到一个名为setup.exe的文件,一个名为Wis.application的文件,当我单击安装程序进行安装时,最终得到的快捷方式为单击一次应用程序引用 C:\用户\ptom\AppData\漫游\Microsoft\Windows\开始菜单\程序\Wis。我尝试将文件与安装创建的快捷方式相关联,并将文件与setup.exe相关联。当我单击文件时,该应用程序启动,但指示
AppDomain.CurrentDomain.SetupInformation.ActivationArguments为空。 (通过指示,我的意思是在文本框中填充我要测试的文本是否为空)。如果我是从调试运行该应用程序的,或者只是从开始菜单运行该应用程序,则它会执行我期望的操作,其遵循的代码路径指示ActivationArguments不为null,但其ActivationData(string [])的长度0。

The code I'm trying is below and at this point all I'm trying to do is put the name of the clicked file in a text box. I suspect my relevant ignorance is about how to reference the click-once application from windows explorer. When I build I end up with a file called setup.exe, a file called Wis.application, and when I click on "setup" to install it, I end up with a shortcut of type "Click-once application reference" in "C:\Users\ptom\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Wis". I've tried associating files with that shortcut created by install, and associating files with setup.exe. When I click on the file, the application launches but indicates that AppDomain.CurrentDomain.SetupInformation.ActivationArguments is null. (By "indicates" I mean the text box gets filled in with the text from where I test to see if it's null). If I run the app from debug, or just by running it from the start menu, it does what I'd expect, following the code path that indicates that ActivationArguments is not null, but that its ActivationData (string[]) is of length 0.

这是app.xaml.cs中的代码

Here is the code from 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";
                }
            }


        }
    }
}

谢谢

推荐答案

首先,您应该为ClickOnce-publish添加文件assoc (选择项目->属性->发布选项->文件关联)

然后添加对 System.Deployment 的引用

然后您可以根据启动类型(ClickOnce或本地)以这种方式提取App.cs中的路径

First of all you should add file assoc for ClickOnce-publish (Select Project->Properties->Publish-Options->File Associations)
Then add Reference to "System.Deployment"
Then you could extract path in App.cs in such a manner, depending on type of startup (ClickOnce or Local)

    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;
    }

这篇关于WPF;单击一次;双击启动文件; VS 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆