/WpInstaller.cs

https://bitbucket.org/CodePorting/testrepofrombitbucket · C# · 197 lines · 153 code · 41 blank · 3 comment · 18 complexity · 58fcda14f5e89d7884908f951bf5f3ac MD5 · raw file

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