PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 8ms app.codeStats 0ms

/WpInstaller.cs

https://bitbucket.org/CodePorting/testrepofrombitbucket
C# | 197 lines | 153 code | 41 blank | 3 comment | 18 complexity | 58fcda14f5e89d7884908f951bf5f3ac MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.ComponentModel;
  6using System.Configuration.Install;
  7using System.Diagnostics;
  8using Microsoft.Win32;
  9using System.Xml;
 10using System.GACManagedAccess;
 11using System.Collections;
 12using System.IO;
 13
 14namespace WebpostingInstaller
 15{
 16    [RunInstaller(true)]
 17    public class WpInstaller : Installer
 18    {
 19
 20        public override void Install(IDictionary stateSaver)
 21        {
 22            base.Install(stateSaver);
 23        }
 24
 25        protected override void OnCommitted(IDictionary savedState)
 26        {
 27            base.OnCommitted(savedState);
 28
 29
 30            string installDirectory = System.IO.Path.GetDirectoryName(Context.Parameters["assemblypath"]);
 31            string exePath = string.Format(@"{0}\BookmarkingBot.exe", installDirectory);
 32
 33            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(exePath);
 34            config.ConnectionStrings.ConnectionStrings["BotEntityContext"].ConnectionString = string.Format(@"metadata=res://*/BookmarkingBot.csdl|res://*/BookmarkingBot.ssdl|res://*/BookmarkingBot.msl;provider=System.Data.SQLite;provider connection string='data source=""{0}\BookmarkingBotDB.sdb""'", installDirectory);
 35            config.Save();
 36
 37
 38
 39            this.InstallSQLiteDataProvider(string.Format(@"{0}\SQLite", installDirectory));
 40        }
 41
 42        protected override void OnBeforeUninstall(IDictionary savedState)
 43        {
 44            base.OnBeforeUninstall(savedState);
 45            this.UninstallSQLiteDataProvider();
 46        }
 47
 48      
 49
 50        private void InstallSQLiteDataProvider(string path)
 51        {
 52            #region Set AssemblyFolder in Registry
 53            RegistryKey sqliteKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\SQLite");
 54
 55            using (RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\SQLite", RegistryKeyPermissionCheck.ReadWriteSubTree))
 56            {
 57                key.SetValue(null, path);
 58            }
 59            #endregion
 60
 61            #region Add factory support to the machine.config file.
 62            try
 63            {
 64                string xmlFileName = Environment.ExpandEnvironmentVariables("%WinDir%\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG\\machine.config");
 65                XmlDocument xmlDoc = new XmlDocument();
 66                xmlDoc.PreserveWhitespace = true;
 67                xmlDoc.Load(xmlFileName);
 68
 69
 70                XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/system.data/DbProviderFactories/add[@invariant=\"System.Data.SQLite\"]");
 71                if (xmlNode == null)
 72                {
 73                    XmlNode xmlConfig = xmlDoc.SelectSingleNode("configuration");
 74                    if (xmlConfig != null)
 75                    {
 76                        XmlNode xmlData = xmlConfig.SelectSingleNode("system.data");
 77                        if (xmlData == null)
 78                        {
 79                            xmlData = xmlDoc.CreateNode(XmlNodeType.Element, "system.data", "");
 80                            xmlConfig.AppendChild(xmlData);
 81                        }
 82                        XmlNode xmlParent = xmlData.SelectSingleNode("DbProviderFactories");
 83                        if (xmlParent == null)
 84                        {
 85                            xmlParent = xmlDoc.CreateNode(XmlNodeType.Element, "DbProviderFactories", "");
 86                            xmlData.AppendChild(xmlParent);
 87                        }
 88
 89                        xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "add", "");
 90                        xmlNode.Attributes.SetNamedItem(xmlDoc.CreateAttribute("name"));
 91                        xmlNode.Attributes.SetNamedItem(xmlDoc.CreateAttribute("invariant"));
 92                        xmlNode.Attributes.SetNamedItem(xmlDoc.CreateAttribute("description"));
 93                        xmlNode.Attributes.SetNamedItem(xmlDoc.CreateAttribute("type"));
 94                        xmlParent.AppendChild(xmlNode);
 95                    }
 96                }
 97
 98                xmlNode.Attributes.GetNamedItem("name").Value = "SQLite Data Provider";
 99                xmlNode.Attributes.GetNamedItem("invariant").Value = "System.Data.SQLite";
100                xmlNode.Attributes.GetNamedItem("description").Value = ".Net Framework Data Provider for SQLite";
101                xmlNode.Attributes.GetNamedItem("type").Value = "System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139";
102
103                xmlDoc.Save(xmlFileName);
104            }
105            catch
106            {
107            }
108            #endregion
109
110            #region Add SQLite Assemblies to Global Assembly Cache
111            string SQLiteLocation = string.Format(@"{0}\System.Data.SQLite.DLL", path);
112            try
113            {
114                AssemblyCache.InstallAssembly(SQLiteLocation, null, AssemblyCommitFlags.Default);
115                AssemblyCache.InstallAssembly(Path.Combine(Path.GetDirectoryName(SQLiteLocation), "System.Data.SQLite.Linq.DLL"), null, AssemblyCommitFlags.Default);
116            }
117            catch
118            {
119
120            }
121            #endregion
122
123        }
124
125        private void UninstallSQLiteDataProvider()
126        {
127            #region Remove AssemblyFolder in Registry
128            RegistryKey sqliteKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\SQLite");
129
130            if (sqliteKey != null)
131            {
132                sqliteKey.Close();
133                Registry.LocalMachine.DeleteSubKey("Software\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\SQLite");
134            }
135            #endregion
136
137            #region  Remove factory support to the machine.config file.
138            string xmlFileName = Environment.ExpandEnvironmentVariables("%WinDir%\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG\\machine.config");
139            XmlDocument xmlDoc = new XmlDocument();
140            xmlDoc.PreserveWhitespace = true;
141            xmlDoc.Load(xmlFileName);
142
143            XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/system.data/DbProviderFactories/add[@invariant=\"System.Data.SQLite\"]");
144
145            if (xmlNode != null)
146                xmlNode.ParentNode.RemoveChild(xmlNode);
147
148            xmlNode = xmlDoc.SelectSingleNode("configuration/system.data/DbProviderFactories/remove[@invariant=\"System.Data.SQLite\"]");
149            if (xmlNode != null)
150                xmlNode.ParentNode.RemoveChild(xmlNode);
151
152            xmlDoc.Save(xmlFileName);
153            #endregion
154
155            #region Remove SQLite Assemblies to Global Assembly Cache
156            try
157            {
158                AssemblyCacheUninstallDisposition disp;
159                AssemblyCacheEnum entries = new AssemblyCacheEnum("System.Data.SQLite");
160                string s;
161
162                while (true)
163                {
164                    s = entries.GetNextAssembly();
165                    if (String.IsNullOrEmpty(s)) break;
166                    AssemblyCache.UninstallAssembly(s, null, out disp);
167                }
168
169                entries = new AssemblyCacheEnum("System.Data.SQLite.Linq");
170
171                while (true)
172                {
173                    s = entries.GetNextAssembly();
174                    if (String.IsNullOrEmpty(s)) break;
175                    AssemblyCache.UninstallAssembly(s, null, out disp);
176                }
177
178            }
179            catch
180            {
181
182            }
183            #endregion
184        }
185
186        private void InitializeComponent()
187        {
188            // 
189            // WpInstaller
190            // 
191           
192
193        }
194
195       
196    }
197}