C# 获取当前ClickOnce';谁的应用程序发布者名称?

C# 获取当前ClickOnce';谁的应用程序发布者名称?,c#,.net,clickonce,C#,.net,Clickonce,是否可以读取当前运行的ClickOnce应用程序的发布者名称(您在Visual Studio中的项目属性->发布->选项->发布者名称中设置的那个) 我之所以需要它,是为了按照本文所述运行当前运行的应用程序的另一个实例,并向其传递参数 当然,我知道我的应用程序的发布者名称,但如果我硬编码它,然后决定更改发布者名称,我很可能会忘记更新这段代码。您可能会认为这很简单,但我在框架中没有看到提供此信息的任何内容 如果你想要一个黑客,你可以从注册表中获得发布者 免责声明-代码丑陋且未经测试 ...

是否可以读取当前运行的ClickOnce应用程序的发布者名称(您在Visual Studio中的
项目属性->发布->选项->发布者名称
中设置的那个)

我之所以需要它,是为了按照本文所述运行当前运行的应用程序的另一个实例,并向其传递参数


当然,我知道我的应用程序的发布者名称,但如果我硬编码它,然后决定更改发布者名称,我很可能会忘记更新这段代码。

您可能会认为这很简单,但我在框架中没有看到提供此信息的任何内容

如果你想要一个黑客,你可以从注册表中获得发布者

免责声明-代码丑陋且未经测试

    ...
    var publisher = GetPublisher("My App Name");
    ...

    public static string GetPublisher(string application)
    {
        using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
        {
            var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == application);
            if (appKey == null) { return null; }
            return GetValue(key, appKey, "Publisher");
        }
    }

    private static string GetValue(RegistryKey key, string app, string value)
    {
        using (var subKey = key.OpenSubKey(app))
        {
            if (!subKey.GetValueNames().Contains(value)) { return null; }
            return subKey.GetValue(value).ToString();
        }
    }

如果您找到更好的解决方案,请跟进。

我不知道ClickOnce,但通常情况下,您可以使用系统读取程序集信息。反射框架:

public string AssemblyCompany
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
    }

不幸的是,没有“publisher”自定义属性,只是把它作为一个可能的解决办法扔掉了这里还有另一个选项。请注意,它将只获取当前运行的应用程序的发布者名称,这就是我所需要的

我不确定这是否是解析XML的最安全的方法

public static string GetPublisher()
{
    XDocument xDocument;
    using (MemoryStream memoryStream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
    using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
    {
        xDocument = XDocument.Load(xmlTextReader);
    }
    var description = xDocument.Root.Elements().Where(e => e.Name.LocalName == "description").First();
    var publisher = description.Attributes().Where(a => a.Name.LocalName == "publisher").First();
    return publisher.Value;
}

在启动ClickOnce应用程序的其他实例时,如果可能的话,我会尝试在您的环境中使用带有querystring参数的激活URL(需要部署到Web服务器)。据我所知,我不确定这是否适用于我的情况。这是从服务器安装的,但据我所知不是从服务器“运行”的。我的意思是你可以在安装后离线运行它。您是否应该将该URL传递给
进程。启动