WPF;单击一次;双击文件启动;对比2008年

发布于 2024-10-03 17:05:07 字数 2244 浏览 0 评论 0原文

我的应用程序仅供我和同事使用,因此我不在乎它是 Click-Once 还是复制 exe。我希望能够在 Windows 资源管理器中单击具有给定扩展名的文件,然后启动我的程序并打开该文件。我无法让它捕获文件名。

表面上的解决方案:

链接

我正在尝试的代码如下,此时我要做的就是将单击的文件的名称放入文本框中。我怀疑我的相关无知是关于如何从 Windows 资源管理器引用单击一次应用程序。当我构建时,我最终会得到一个名为 setup.exe 的文件,一个名为 Wis.application 的文件,当我单击“setup”来安装它时,我最终会在“中得到一个类型为“单击一次应用程序引用”的快捷方式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";
                }
            }


        }
    }
}
            

谢谢

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.

Ostensible solution:

Link

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.

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


        }
    }
}
            

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-10-10 17:05:07

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

然后添加对“System.Deployment”的引用

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

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

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;
    }
ヤ经典坏疍 2024-10-10 17:05:07

您检查过Environment.GetCommandLineArgs() 吗?
这显示了您的应用程序启动时使用的参数。

如果您的应用程序与文件关联,则它应包含文件名作为参数之一。

Have you checked Environment.GetCommandLineArgs()?
That shows the arguments with which your application was started.

If your application is associated with a file, it should contain the filename as one of the arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文