Detect current VSIX package's version from code

允我心安 提交于 2019-12-19 10:15:12

问题


I am writing a VSIX project and I would like for the code to be able to determine whether an update is available.

I know Visual Studio would be able to check for the update, however, I would like for the extension to be able to prompt user (developer) more verbosely.

  1. I wonder how could make the extension read its own version from the package manifest?

Thank you.


回答1:


I found that I could read the version information directly from the manifest XML file.

        var doc = new XmlDocument();
        doc.Load(manifestPath);
        var metaData = doc.DocumentElement.ChildNodes.Cast<XmlElement>().First(x => x.Name == "Metadata");
        var identity = metaData.ChildNodes.Cast<XmlElement>().First(x => x.Name == "Identity");
        var version = identity.GetAttribute("Version");

I also wrote a gist C# class code that encapsulate the code above. Besides, version, this technique could be used to get other information provided by the manifest file.




回答2:


Nordin's solution seems good, but I just want to mention that there is one more way to get current version of the extension. I have no idea in what situation my solution might be better, maybe if you don't know the path to the manifest on the client that uses this extension.

// get ExtensionManager
IVsExtensionManager manager = GetService(typeof(SVsExtensionManager)) as IVsExtensionManager;
// get your extension by Product Id
IInstalledExtension myExtension = manager.GetInstalledExtension("ProductId-1234-1234-1234-123456789012");
// get current version
Version currentVersion = myExtension.Header.Version;

I call this inside Initialize() method of my package.



来源:https://stackoverflow.com/questions/22666144/detect-current-vsix-packages-version-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!