c# - 如何以编程方式执行已安装的 clickonce 应用程序
问题描述
我有一个通过 ClickOnce 发布的 Windows 应用程序。我也有带有 Application Files 文件夹的 .exe 和 .application 文件。我的应用程序安装成功,我的桌面上有 .appref-ms 文件以及成功的午餐应用程序。
现在我想通过我的 C# 代码或 JavaScript 代码运行这个已安装的应用程序。我通过 C# 代码通过 Process.Start(appPath, argsToPass); 实现了这一点 但这项工作只有我的 Web 应用程序在 IIS Express 模式下运行。当我在本地 IIS 上切换我的应用程序并创建虚拟目录时,Process.Start(appPath, argsToPass) 不会启动我的应用程序。
public void lunchClickOnce()
{
string applicationReferencePath = @"C:\Users\myUserName\Desktop\myDesktopApp.appref-ms";
//If application is not Installed/found
if (!System.IO.File.Exists(applicationReferencePath))
{
string url = "http://myweb.com/ClickOnce/myDesktopApp.application";
byte[] applicationManifest = null;
string applicationReference = null;
// Download the application manifest.
using (WebClient wc = new WebClient())
{
applicationManifest = wc.DownloadData(url);
}
// Build the .appref-ms contents.
using (MemoryStream stream = new MemoryStream(applicationManifest))
{
XNamespace asmv1 = "urn:schemas-microsoft-com:asm.v1";
XNamespace asmv2 = "urn:schemas-microsoft-com:asm.v2";
XDocument doc = XDocument.Load(stream);
XElement assembly = doc.Element(asmv1 + "assembly");
XElement identity = assembly.Element(asmv1 + "assemblyIdentity");
XElement deployment = assembly.Element(asmv2 + "deployment");
XElement provider = deployment.Element(asmv2 + "deploymentProvider");
XAttribute codebase = provider.Attribute("codebase");
XAttribute name = identity.Attribute("name");
XAttribute language = identity.Attribute("language");
XAttribute publicKeyToken = identity.Attribute("publicKeyToken");
XAttribute architecture = identity.Attribute("processorArchitecture");
applicationReference = String.Format(
"{0}#{1}, Culture={2}, PublicKeyToken={3}, processorArchitecture={4}",
codebase.Value,
name.Value,
language.Value,
publicKeyToken.Value,
architecture.Value);
}
// Write the .appref-ms file (ensure that it's Unicode encoded).
using (FileStream stream = System.IO.File.OpenWrite(applicationReferencePath))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(applicationReference);
}
}
}
string argsToPass = "ArgsToSend";
//Start/Install the application
Process.Start(applicationReferencePath, argsToPass);
}
预期的输出是我的代码成功运行/启动 clickonce 安装的应用程序。
解决方案
推荐阅读
- docker - docker-compose 在多个服务之间共享本地绑定卷
- android - 如何将 dataBinding 与 spinner 一起使用并获取当前值?
- python - 将多个散景 html 文件添加到 django 模板
- c# - 如何在网格视图 RowCreated 事件中获取 ID 值
- javascript - 如何在反应中传递状态?
- r - tapply 和 mean 提供不同的答案
- java - 如何在 Netbeans 11 中使用 Maven 和 Inno Setup 为 JavaFX 应用程序创建可执行安装程序
- excel - 插入单元格时如何保持公式使用相同的引用?
- php - 如何将png文件转换为webp文件
- python - 如何区分 ValueError 的情况