我可以从应用程序内部获取 ClickOnce 发布的产品名称吗?

tof*_*tim 5 c# installation clickonce

我的 ClickOnce 发布名称与程序集名称不同。出于讨论目的,它是“App 6.0”。我在项目的属性中设置了它。有什么办法可以从程序内部获取这个值吗?

Mag*_*der 5

添加对 的引用Microsoft.Build.Tasks.v4.0.dll,然后运行:

if (null != AppDomain.CurrentDomain.ActivationContext)
{
    DeployManifest manifest;
    using (MemoryStream stream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
    {
        manifest = (DeployManifest)ManifestReader.ReadManifest("Deployment", stream, true);
    }
    // manifest.Product has the name you want
}
else
{
   // not deployed
}
Run Code Online (Sandbox Code Playgroud)

DeployManifest 还可以提供清单中的其他有用信息,例如 Publisher 或 SupportUrl。


tof*_*tim 2

答案可以在ClickOnce Run at Startup中找到。本质上,您使用 InPlaceHostingManager 获取 ClickOnce 清单并读取它。它是一个异步方法,这让我感到烦恼,但这是迄今为止唯一有效的方法。非常感谢简化。请参阅网页以获取 DeploymentDescription 的说明。

var inPlaceHostingManager = new InPlaceHostingManager(ApplicationDeployment.CurrentDeployment.UpdateLocation, false);
inPlaceHostingManager.GetManifestCompleted += ((sender, e) =>
{
    try
    {
        var deploymentDescription = new DeploymentDescription(e.DeploymentManifest);
        string productName = deploymentDescription.Product;
        ***DoSomethingToYour(productName);***

        // - use this later -
        //var commandBuilder = new StartMenuCommandBuilder(deploymentDescription);
        //string startMenuCommand = commandBuilder.Command;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
    }
});
Run Code Online (Sandbox Code Playgroud)