Click here to Skip to main content
15,892,161 members
Articles / Desktop Programming / WPF

StringsResourceGenerator, Custom Task + Add-in for managing strings in ResourceDictionary from code

Rate me:
Please Sign up or sign in to vote.
4.05/5 (8 votes)
27 May 2008CPOL4 min read 28.4K   291   10  
Creates a XAML file for your strings and generates a class to simplify use from code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.ComponentModel;

namespace StringsResourceGeneratorSetupAction
{
    [RunInstaller(true)]
    public class AddinInstaller : Installer
    {
        #region Data Members

        private const string ExtNameSpace = "http://schemas.microsoft.com/AutomationExtensibility";

        #endregion

        #region Constructors

        public AddinInstaller()
        {
        }

        #endregion

        #region Public Functions

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            // Uncomment the following line, recompile the setup
            // project and run the setup executable if you want
            // to debug into this custom action.

            //Debugger.Break();

            base.Install(stateSaver);

            // Parameters required to pass in from installer

            string productName = this.Context.Parameters["ProductName"];
            string assemblyName = this.Context.Parameters["AssemblyName"];

            // Setup .addin path and assembly path

            string addinTargetPath = Path.Combine(Environment.GetFolderPath(
                   Environment.SpecialFolder.MyDocuments),
                   @"Visual Studio 2008\Addins");
            string assemblyPath = Path.GetDirectoryName(
                   System.Reflection.Assembly.GetExecutingAssembly().Location);
            string addinControlFileName = assemblyName + ".Addin";
            string addinAssemblyFileName = assemblyName + ".dll";

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(addinTargetPath);
                if (!dirInfo.Exists)
                {
                    dirInfo.Create();
                }

                string sourceFile = Path.Combine(assemblyPath, addinControlFileName);
                XmlDocument doc = new XmlDocument();
                doc.Load(sourceFile);
                XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
                xnm.AddNamespace("def", ExtNameSpace);

                // Update Addin/Assembly node

                XmlNode node = doc.SelectSingleNode("/def:" +
                    "Extensibility/def:Addin/def:Assembly", xnm);
                if (node != null)
                {
                    node.InnerText = Path.Combine(assemblyPath, addinAssemblyFileName);
                }

                // Update ToolsOptionsPage/Assembly node

                node = doc.SelectSingleNode("/def:Extensibility/def:" +
                       "ToolsOptionsPage/def:Category/def:SubCategory/def:Assembly", xnm);
                if (node != null)
                {
                    node.InnerText = Path.Combine(assemblyPath, addinAssemblyFileName);
                }

                doc.Save(sourceFile);

                string targetFile = Path.Combine(addinTargetPath, addinControlFileName);
                File.Copy(sourceFile, targetFile, true);

                // Save AddinPath to be used in Uninstall or Rollback

                stateSaver.Add("AddinPath", targetFile);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        public override void Rollback(System.Collections.IDictionary savedState)
        {
            //Debugger.Break();

            base.Rollback(savedState);

            try
            {
                string fileName = (string)savedState["AddinPath"];
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            //Debugger.Break();

            base.Uninstall(savedState);

            try
            {
                string fileName = (string)savedState["AddinPath"];
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        #endregion
    }
}

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
Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions