Click here to Skip to main content
15,895,779 members
Articles / Programming Languages / XML

XML Schema Reader Writer Plugin for VS 2005/2008

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
17 Apr 2009CPOL3 min read 33K   716   11  
Schema based XML reader writer implemented as .NET COM generator
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Xml;


namespace PostInstaller
{
    [RunInstaller(true)]
    public partial class PostInstaller : Installer
    {
        public PostInstaller()
        {
            InitializeComponent();
            this.BeforeUninstall += new InstallEventHandler(PostInstaller_BeforeUninstall);
        }

        void PostInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            string vsCfgInstallPath = GetVsAddInPath("8.0");
            if( vsCfgInstallPath != null )
            {
                if (File.Exists(vsCfgInstallPath))
                    File.Delete(vsCfgInstallPath);
            }

            vsCfgInstallPath = GetVsAddInPath("9.0");
            if (vsCfgInstallPath != null)
            {
                if (File.Exists(vsCfgInstallPath))
                    File.Delete(vsCfgInstallPath);
            }
        }

     
        public override void Install(IDictionary savedState)
        {
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("devenv");
            if( null != process && process.Length > 0)
                throw new InstallException("Instance of visual studio was detected, all instances must be closed before installation");

            if (null == GetVsAddInPath("9.0") && null == GetVsAddInPath("8.0") )
                throw new InstallException("Visual studio 2005/ 2008 installation wasn't found");
                
            base.Install(savedState);

            // get the source directory of the installation from the default context parameters
            string TheAssemblyPath = this.Context.Parameters["assemblypath"];
            TheAssemblyPath = Path.GetDirectoryName(TheAssemblyPath) + Path.DirectorySeparatorChar;

            SavePluginCfgFile(TheAssemblyPath);
        }


        static string GetVsAddInPath(string vsVersion)
        {
            string regKey = string.Format(@"HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\{0}", vsVersion);
            string vsCfgPath = (string)Microsoft.Win32.Registry.GetValue(regKey, "VisualStudioLocation", null);

            if (vsCfgPath == null) return null;//VS not installed

            
            vsCfgPath = vsCfgPath + Path.DirectorySeparatorChar + "Addins";
            if (!Directory.Exists(vsCfgPath))
                Directory.CreateDirectory(vsCfgPath);
            
            vsCfgPath += Path.DirectorySeparatorChar + "XmlSchemaAddon.AddIn";
            return vsCfgPath;
        }

        static void SavePluginCfgFile(string installPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load( new MemoryStream(Properties.Resources.XmlSchemaAddon) );
            string strValue = xmlDoc.DocumentElement["Addin"]["Assembly"].InnerText;
            xmlDoc.DocumentElement["Addin"]["Assembly"].InnerText = installPath + strValue;

            //saving as VS 2008 plugin

            string cfgFilePath = GetVsAddInPath("9.0");
            if (null != cfgFilePath )
            {
                foreach (XmlElement child in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (child == null || 0 != string.Compare(child.Name, "HostApplication", true))
                        continue;

                    child["Version"].InnerText = "9.0";
                }
                xmlDoc.Save(cfgFilePath);
            }                  

            //saving as VS 2005 plugin

            cfgFilePath = GetVsAddInPath("8.0");
            if( null != cfgFilePath )
            {
                foreach (XmlElement child in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (child == null || 0 != string.Compare(child.Name, "HostApplication", true))
                        continue;

                    child["Version"].InnerText = "8.0";
                }
                xmlDoc.Save(cfgFilePath);
            }            
        }
    }


   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Israel Israel
Born and raised in Israel, caught the programming virus at the age of 15.
Since than I can't stop coding.

Comments and Discussions