首页 > 解决方案 > 如何以编程方式执行已安装的 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 安装的应用程序。

标签: c#asp.netasp.net-mvcprocessclickonce

解决方案


推荐阅读