/[AnywhereTS-MSSQL]/trunk/TSAdminTool/ATSAmdin.Installer.cs
ViewVC logotype

Annotation of /trunk/TSAdminTool/ATSAmdin.Installer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 184 - (hide annotations) (download)
Mon Jul 16 14:05:59 2012 UTC (8 years, 7 months ago) by william
File size: 14454 byte(s)
remove duplicated registry registry

1 william 145 using System;
2     using System.Collections;
3     using System.Collections.Generic;
4     using System.ComponentModel;
5     using System.Configuration.Install;
6     using System.Linq;
7 william 149 using System.Reflection;
8 william 152 using System.Threading;
9     using System.Windows.Forms;
10     using System.Drawing;
11 william 157 using AnywhereTS.DBSupport;
12     using System.Data.SqlClient;
13 william 145
14    
15     namespace AnywhereTS
16     {
17     [RunInstaller(true)]
18     public partial class ATSAmdin : System.Configuration.Install.Installer
19     {
20 william 152 string DBServer;
21     string DBInstance;
22 william 145 public ATSAmdin()
23     {
24     InitializeComponent();
25     }
26 william 177
27     protected override void OnBeforeInstall(IDictionary savedState)
28     {
29     base.OnBeforeInstall(savedState);
30     }
31    
32 william 157 #region Install Members
33 william 152 DatabaseInstallerWaitDialog dlg;
34     public void InstallDatabaseWaitMessage()
35     {
36     dlg = new DatabaseInstallerWaitDialog("", string.Format(@"Please Wait... Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName));
37     dlg.ForeColor = Color.Black;
38     dlg.ShowDialog();
39 william 157 }
40 william 153 private void InstallDatabase()
41     {
42     // install the database
43     Thread db_installer_thread = new Thread(new ThreadStart(InstallDatabaseWaitMessage));
44     db_installer_thread.IsBackground = true;
45     db_installer_thread.Start();
46 william 183 Logging.ATSAdminInstallerLog.InfoFormat(@"Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
47 william 153 AnywhereTS.DatabaseSupport dbsup = new DatabaseSupport();
48     dbsup.SetupDatabase();
49     dlg.Message = string.Format(@"Successfully installed the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
50     dlg.ForeColor = Color.Green;
51     Thread.Sleep(new TimeSpan(0, 0, 15));
52     dlg.Close();
53 william 183 Logging.ATSAdminInstallerLog.InfoFormat(@"Successfully Installed database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
54 william 153 }
55    
56     private void ConfigureATS()
57     {
58     try
59     {
60 william 183 Logging.ATSAdminInstallerLog.Info("Configuring AnywhereTS");
61 william 153 frmAdmin atsadmin = new frmAdmin();
62     if (!atsadmin.ConfigureATS())
63     {
64     throw new Exception("AnywhereTS cannot be installed without configuring it first.");
65     }
66 william 183 Logging.ATSAdminInstallerLog.Info("Successfully Configured AnywhereTS");
67 william 153 }
68     catch (Exception ex)
69     {
70     // database install failed
71     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
72     {
73     Logging.ATSAdminInstallerLog.Error("ConfigureATS() failed.");
74     }
75     throw ex;
76     }
77     }
78    
79 william 149 public override void Install(IDictionary stateSaver)
80     {
81     base.Install(stateSaver);
82 william 150 try
83     {
84 william 152
85 william 150 string path = this.Context.Parameters["targetdir"];
86     Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
87     using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
88     {
89     // setup database
90 william 152 DBServer = this.Context.Parameters["DBSERVER"];
91     DBInstance = this.Context.Parameters["DBINSTANCE"];
92 william 169 try
93     {
94 william 174 string server = string.Empty;
95     string instance = string.Empty;
96    
97     try { server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer); }
98     catch { }
99     try { instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance); }
100     catch { }
101     Logging.ATSAdminInstallerLog.DebugFormat("RegServer={0} RegInstance={1}", server, instance);
102    
103     if (!string.IsNullOrEmpty(server))
104     DBServer = server;
105     if (!string.IsNullOrEmpty(instance))
106     DBInstance = instance;
107 william 169 }
108     catch
109     {
110     DBServer = this.Context.Parameters["DBSERVER"];
111     DBInstance = this.Context.Parameters["DBINSTANCE"];
112     }
113    
114    
115 william 150 // Log the entered data
116     Logging.ATSAdminInstallerLog.DebugFormat("Server={0} Instance={1}", DBServer, DBInstance);
117     CreateRegistryConfigKeys();
118     ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseServer, DBServer);
119     ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseInstance, DBInstance);
120 william 152 ProSupport.strDatabaseServer = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
121     ProSupport.strDatabaseInstance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
122 william 153
123     InstallDatabase();
124     ConfigureATS();
125 william 149 }
126 william 150 }
127     catch (Exception ex)
128     {
129 william 152 Logging.ATSAdminInstallerLog.DebugFormat(@"Failed to Install database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
130     dlg.Message = string.Format(@"Failed to install the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
131     dlg.ForeColor = Color.Red;
132     Thread.Sleep(new TimeSpan(0, 0, 15));
133     dlg.Close();
134 william 150 // database install failed
135     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
136 william 149 {
137 william 150 Logging.ATSAdminInstallerLog.Error("Install() failed.");
138 william 149 }
139 william 150 throw ex;
140 william 149 }
141     }
142    
143     private void CreateRegistryConfigKeys()
144     {
145 william 150 try
146     {
147 william 183 Logging.ATSAdminInstallerLog.Info("Creating AnywhereTS Admin registry settings");
148 william 150 Microsoft.Win32.RegistryKey key = null;
149     if (IntPtr.Size == 8)
150     { // 64 bit OS
151     //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
152     key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
153     key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
154     key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
155     }
156     else
157     { // 32 bit OS
158     //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
159     key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
160     key.CreateSubKey("TFTPD32").CreateSubKey("DHCP");
161     key = key.CreateSubKey(ATSGlobals.ApplicationName).CreateSubKey("ts-config");
162     }
163    
164     // AdminVersion = 2
165     key.SetValue("AdminVersion", 2);
166     // RunFirstTime = 1
167     key.SetValue("RunFirstTime", 1);
168     ATSGlobals.CreateRegistryValues();
169 william 184 Logging.ATSAdminInstallerLog.Info("Successfully created AnywhereTS Admin registry settings");
170 william 149 }
171 william 150 catch (Exception ex)
172     {
173 william 152 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
174 william 150 {
175     Logging.ATSAdminInstallerLog.Error("CreateRegistryConfigKeys() failed.");
176     }
177 william 149 }
178     }
179 william 157 #endregion
180    
181 william 177 #region uninstall
182 william 157 public override void Uninstall(IDictionary savedState)
183     {
184     using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
185     {
186     try
187     {
188     string path = this.Context.Parameters["targetdir"];
189     Logging.ATSAdminInstallerLog.DebugFormat("Install Dir: {0}", path);
190     Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
191     Logging.ATSAdminInstallerLog.DebugFormat("Showing uninstall dialog");
192     UninstallDialog udlg = new UninstallDialog();
193     DialogResult result = udlg.ShowDialog();
194     if (result == DialogResult.Cancel)
195     {
196     Logging.ATSAdminInstallerLog.DebugFormat("user aborted uninstall");
197     MessageBox.Show("Uninstall canceled ... nothing was uninstalled", "Uinstall was canceled by user", MessageBoxButtons.OK, MessageBoxIcon.Warning);
198     return;
199     }
200     base.Uninstall(savedState);
201     using (log4net.NDC.Push(string.Format("KeepDatabase={0} KeepApplicationSettings={1} KeepClientImages={2}", udlg.KeepDatabase, udlg.KeepApplicationSettings, udlg.KeepClientImages)))
202     {
203     Logging.ATSAdminInstallerLog.DebugFormat("Unistall options");
204     }
205     string server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
206     string instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
207     string database = ATSGlobals.strDatabaseName;
208     if (!udlg.KeepDatabase)
209     {
210     Exception ErrorInfo = null;
211     // delete database
212     using (MsSqlConnector conn = new MsSqlConnector(server, instance, "master"))
213     {
214     try
215     {
216     conn.CreateConnection(out ErrorInfo);
217     conn.OpenConnection(out ErrorInfo);
218     SqlConnection sqlCon;
219     conn.GetConnectionClone(out sqlCon, out ErrorInfo);
220     SqlCommand sqlCmd = new SqlCommand();
221     sqlCmd.Connection = sqlCon;
222     Logging.ATSAdminInstallerLog.DebugFormat("Removing database: {0}", database);
223     sqlCmd.CommandText = string.Format("DROP DATABASE {0};",database);
224     sqlCmd.ExecuteNonQuery();
225     Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed database: {0}", database);
226     }
227     catch (Exception ex)
228     {
229     Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove database: {0}", database);
230     ErrorInfo = ex;
231     }
232     }
233     if (ErrorInfo != null)
234     throw ErrorInfo;
235     }
236     if (!udlg.KeepApplicationSettings)
237     {
238     // delete registry settings
239     try
240     {
241     Logging.ATSAdminInstallerLog.DebugFormat("Removing AnywhereTS Registry settings");
242     Microsoft.Win32.RegistryKey key = null;
243     if (IntPtr.Size == 8)
244     { // 64 bit OS
245     //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
246     key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
247 william 160 key.DeleteSubKeyTree("TFTPD32", false);
248     key.DeleteSubKeyTree(ATSGlobals.ApplicationName, false);
249 william 157 }
250     else
251     { // 32 bit OS
252     //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
253     key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
254 william 160 key.DeleteSubKeyTree("TFTPD32", false);
255     key.DeleteSubKeyTree(ATSGlobals.ApplicationName, false);
256 william 157 }
257     Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed AnywhereTS Registry settings");
258     }
259     catch (Exception ex)
260     {
261     Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove AnywhereTS Registry settings");
262     throw ex;
263     }
264     }
265     if (!udlg.KeepClientImages)
266     {
267     // delete client images
268     try
269     {
270 william 158 Logging.ATSAdminInstallerLog.Warn("Deleteing Client Images has not been implemented yet.");
271 william 157 }
272     catch (Exception ex)
273     {
274 william 158 throw ex;
275 william 157 }
276     }
277     }
278     catch (Exception ex)
279     {
280     // database install failed
281     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
282     {
283     Logging.ATSAdminInstallerLog.Error("Install() failed.");
284     }
285     throw ex;
286     }
287     }
288     }
289 william 177 #endregion
290 william 145 }
291     }

  ViewVC Help
Powered by ViewVC 1.1.22