我的应用程序仅供我和同事使用,因此我不在乎它是 Click-Once 还是复制 exe。我希望能够在 Windows 资源管理器中单击具有给定扩展名的文件,然后启动我的程序并打开该文件。我无法让它捕获文件名。
表面上的解决方案:
Link
我正在尝试的代码如下,此时我要做的就是将单击的文件的名称放入文本框中。我怀疑我的相关无知是关于如何从 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";
}
}
}
}
}
Thanks
首先,您应该为 ClickOnce-publish 添加文件关联(选择项目->属性->发布选项->文件关联)
然后添加引用“系统部署"
然后,您可以根据启动类型(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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)