Detect current VSIX package's version from code
Asked Answered
Z

3

5

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.

Zincate answered 26/3, 2014 at 15:42 Comment(5)
Can't you just pull the version information from the immediate assembly?Backstay
@Lloyd, thanks for the reply. The immediate assembly's version info is not synch with vsix's version info. To keep that in sync might be a good idea too. Thanks.Zincate
Productivity Power Tools for Visual Studio 2010 provide status tray notifications of extension updates. This became a standard feature starting in Visual Studio 2012. Do not reimplement this feature in a non-standard manner; rather, inform your users about the existing feature.Steeple
@280Z28, thanks for the feedback and advice. As stated above, I am fully aware about the standard Visual Studio notification. Regardless of that (to reimplement that or not), my question still stands.Zincate
Now how can we get the current extension version on Visual Studio Gallery to compare with the local version?Quintessence
Z
9

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.

Zincate answered 18/4, 2014 at 11:7 Comment(3)
Thanks man, just was trying to find solution to that question too. You saved a lot of time for meTanatanach
@AliSharabiani You may want to review the gist I provide in the answer above. It should be (at least on my case) something like this: Path.Combine(assemblyDirectory, "extension.vsixmanifest")Zincate
@Zincate I tried that, it is same as using this code: manifestPath = "extension.vsixmanifest" (paths are relative in vspackage, that's why this also works). It works locally, however, when deploying the package on Visual Studio Gallery and installing it; the path is wrong Could not find file C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\extension.vsixmanifest.Quintessence
C
7

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.

Cenis answered 5/5, 2014 at 18:10 Comment(4)
One thing missing, the dll for this isn't visible as a reference to add, you need to add it via browse from the following location - C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.ExtensionManager\{your version you want to use}\Microsoft.VisualStudio.ExtensionManager.dllUnpen
Works like a champ! thanks! Tested on both Visual Studio Experimental Instance and also after deploying the package.Quintessence
This solution doesn't involve file system calls, which is better in some cases. You just take the value from the memory.Turco
Am I missing something. What is GetService on the first line? Can you shared that code?Scepter
V
2

This code motivated by Wayne Koorts works for me, even if deploying the package on Visual Studio Gallery:

    private string getVsixVersion()
    {
        var asm = Assembly.GetExecutingAssembly();
        var asmDir = Path.GetDirectoryName(asm.Location);
        var manifestPath = Path.Combine(asmDir, "extension.vsixmanifest");
        var version = "?";
        if (File.Exists(manifestPath))
        {
            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");
            version = identity.GetAttribute("Version");

        }
        return version;
    }
Vermicular answered 12/10, 2021 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.