PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/PrerequisiteInstaller/InstallPrerequisite.cs

#
C# | 279 lines | 241 code | 24 blank | 14 comment | 25 complexity | 7c2b81021184cef328cdadb76b5c7654 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Deployment;
  12. using Microsoft.Win32;
  13. using System.Security.Permissions;
  14. using System.Globalization;
  15. [assembly: CLSCompliant(true)]
  16. [assembly: SecurityPermission(
  17. SecurityAction.RequestMinimum, Execution = true)]
  18. namespace PrerequisiteInstaller
  19. {
  20. public partial class InstallPrerequisite : Form
  21. {
  22. public InstallPrerequisite()
  23. {
  24. InitializeComponent();
  25. Application.EnableVisualStyles();
  26. }
  27. static string programFilesLocation = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  28. private void InstallPrerequisite_Load(object sender, EventArgs e)
  29. {
  30. this.pnlInstallingPrerequisite.Visible = true;
  31. this.pnlInstallationComplete.Visible = false;
  32. lblPrerequisiteMsg.Text = "Checking for prerequisites...";
  33. UpdateProgramFilesPathFor64BitMachine();
  34. }
  35. private void InstallPrerequisite_Activated(object sender, EventArgs e)
  36. {
  37. this.Refresh();
  38. }
  39. private static void UpdateProgramFilesPathFor64BitMachine()
  40. {
  41. try
  42. {
  43. if (!programFilesLocation.Contains("x86"))
  44. {
  45. if (Directory.Exists(programFilesLocation.TrimEnd() + " (x86)"))
  46. {
  47. programFilesLocation = programFilesLocation.Replace("Program Files", "Program Files (x86)");
  48. }
  49. }
  50. }
  51. catch (Exception)
  52. {
  53. //Failed to update program files path
  54. }
  55. }
  56. private void InstallPrerequisite_Shown(object sender, EventArgs e)
  57. {
  58. try
  59. {
  60. this.BringToFront();
  61. this.TopLevel = true;
  62. this.Refresh();
  63. if (!IsWindowsInstallerInstalled())
  64. {
  65. lblPrerequisiteMsg.Text = "Prerequisite for Ontology Recognition add-in is being installed...";
  66. InstallWindowsInstaller();
  67. }
  68. else
  69. {
  70. //eventLogger.WriteToLog(LogCategory.Installer, Resource.PRIMINTEROPALREADYINSTALLED);
  71. //Log: Primary Interop Assemblies are already installed
  72. }
  73. this.pnlInstallingPrerequisite.Visible = false;
  74. this.pnlInstallationComplete.Visible = true;
  75. lblBannerHeading.Text = "Installation Complete";
  76. Font font = new Font("Tahoma", 13);
  77. lblBannerHeading.Font = font;
  78. btnFinish.Enabled = true;
  79. btnFinish.Text = "&Finish";
  80. btnFinish.Focus();
  81. }
  82. catch (Exception)
  83. {
  84. //Failed to show Finish Dailog
  85. }
  86. }
  87. private void InstallWindowsInstaller()
  88. {
  89. try
  90. {
  91. string osName = GetOSName();
  92. string installationPath = Path.Combine(programFilesLocation, "Word Add in For Ontology Recognition");
  93. //MessageBox.Show(osName);
  94. //MessageBox.Show(installationPath);
  95. switch (osName)
  96. {
  97. case "Windows Vista":
  98. if (Environment.OSVersion.Platform.ToString().Contains("32"))
  99. {
  100. //Install
  101. //x86 Platform: Windows6.0-KB942288-v2-x86.msu (1.7 MB)
  102. Process installProcess = new Process();
  103. installProcess.StartInfo.FileName = installationPath + @"\Prerequisites\Windows6.0-KB942288-v2-x86.msu";
  104. installProcess.Start();
  105. while (!installProcess.HasExited)
  106. {
  107. System.Threading.Thread.Sleep(1000);
  108. }
  109. }
  110. else if (Environment.OSVersion.Platform.ToString().Contains("64"))
  111. {
  112. //Install
  113. //x64 Platform: Windows6.0-KB942288-v2-x64.msu (2.9 MB)
  114. Process installProcess = new Process();
  115. installProcess.StartInfo.FileName = installationPath + @"\Prerequisites\Windows6.0-KB942288-v2-x64.msu";
  116. installProcess.Start();
  117. while (!installProcess.HasExited)
  118. {
  119. System.Threading.Thread.Sleep(1000);
  120. }
  121. }
  122. break;
  123. case "Windows XP":
  124. {
  125. //Install
  126. //x86 Platform: WindowsXP-KB942288-v3-x86.exe (3.2 MB)
  127. Process installProcess = new Process();
  128. installProcess.StartInfo.FileName = installationPath + @"\Prerequisites\WindowsXP-KB942288-v3-x86.exe";
  129. installProcess.Start();
  130. while (!installProcess.HasExited)
  131. {
  132. System.Threading.Thread.Sleep(1000);
  133. }
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. catch (Exception)
  141. {
  142. }
  143. }
  144. private bool IsWindowsInstallerInstalled()
  145. {
  146. string filepath = Environment.GetFolderPath(Environment.SpecialFolder.System);
  147. filepath = Path.Combine(filepath, "msiexec.exe");
  148. if (File.Exists(filepath))
  149. {
  150. FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(filepath);
  151. if (!string.IsNullOrEmpty(fileVersionInfo.FileVersion) &&
  152. (fileVersionInfo.ProductVersion.StartsWith("4.5") ||
  153. fileVersionInfo.ProductVersion.StartsWith("4.0") ||
  154. fileVersionInfo.ProductVersion.StartsWith("3.5") ||
  155. fileVersionInfo.ProductVersion.StartsWith("3.1")))
  156. {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. private static string GetOSName()
  163. {
  164. OperatingSystem osInfo = Environment.OSVersion;
  165. string osName = "UNKNOWN";
  166. try
  167. {
  168. switch (osInfo.Platform)
  169. {
  170. case PlatformID.Win32Windows:
  171. {
  172. switch (osInfo.Version.Minor)
  173. {
  174. case 0:
  175. {
  176. osName = "Windows 95";
  177. break;
  178. }
  179. case 10:
  180. {
  181. if (osInfo.Version.Revision.ToString(CultureInfo.CurrentCulture) == "2222A")
  182. {
  183. osName = "Windows 98 Second Edition";
  184. }
  185. else
  186. {
  187. osName = "Windows 98";
  188. }
  189. break;
  190. }
  191. case 90:
  192. {
  193. osName = "Windows Me";
  194. break;
  195. }
  196. }
  197. break;
  198. }
  199. case PlatformID.Win32NT:
  200. {
  201. switch (osInfo.Version.Major)
  202. {
  203. case 3:
  204. {
  205. osName = "Windows NT 3.51";
  206. break;
  207. }
  208. case 4:
  209. {
  210. osName = "Windows NT 4.0";
  211. break;
  212. }
  213. case 5:
  214. {
  215. if (osInfo.Version.Minor == 0)
  216. {
  217. osName = "Windows 2000";
  218. }
  219. else if (osInfo.Version.Minor == 1)
  220. {
  221. osName = "Windows XP";
  222. }
  223. else if (osInfo.Version.Minor == 2)
  224. {
  225. osName = "Windows Server 2003";
  226. }
  227. break;
  228. }
  229. case 6:
  230. case 7:
  231. {
  232. osName = "Windows Vista";
  233. break;
  234. }
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. catch (NullReferenceException ex)
  241. {
  242. //Failed to get type of operating system.
  243. }
  244. catch (Exception ex)
  245. {
  246. //Failed to get type of operating system.
  247. }
  248. return osName;
  249. }
  250. private void btnFinish_Click(object sender, EventArgs e)
  251. {
  252. this.Close();
  253. }
  254. }
  255. }