ServiceManagerInstaller.cs 7.5 KB
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using Microsoft.Win32;

namespace ServiceManager
{
    [RunInstaller(true)]
    public partial class ServiceManagerInstaller : Installer
    {
		// 获取硬盘物理序列号
		[DllImport("DiskSerialNo.dll")]
		private static extern string GetDiskSerialNo();
		
		// 获取网卡序列号		
		[DllImport("DiskSerialNo.dll")]
		private static extern string GetMacAddr();

        public ServiceManagerInstaller()
		{
			// 该调用是设计器所必需的。
			InitializeComponent();
		}

		public override void Install(IDictionary mySavedState)
		{
            base.Install(mySavedState);	// 安装
            string strdbserver = "", strdbname = "", strdbuser = "", strdbpassword = "", strMyCMMIDBUser = "", strMyCMMIDBPassword = "";
            IISManager ismang = new IISManager();
            DialogResult dr;

            WebConfig m_wc = new WebConfig();
            m_wc.WebConfigPath = this.Context.Parameters["tdname"] + "WebService\\Web.Config";
            ismang.Connect();

            try
            {
                #region 建立IIS
                // 设置WebService权限
                MachineConfiguration mc = new MachineConfiguration();

                if (!mc.ProcessModelSetUserName())
                {
                    this.Rollback(mySavedState);
                    throw new Exception(".Net FrameWork 出现异常或版本不对,无法完成系统配置。请重装.Net FrameWork 2.0");
                }
                if (!ismang.CreateVirtualDirectory(this.Context.Parameters["vdname"], this.Context.Parameters["tdname"] + "WebService"))
                {
                    dr = MessageBox.Show("服务器上虚拟目录 " + this.Context.Parameters["vdname"] + " 已存在,\r\n继续安装请按‘确定’,虚拟目录将被重新建立。\r\n退出安装请按‘取消’。", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    //MessageBox.Show(dr.ToString());

                    if (dr == DialogResult.Cancel)
                    {
                        this.Rollback(mySavedState);
                        throw new Exception("用户取消了安装,安装未完成。");
                    }
                    else
                    {
                        try
                        {
                            ismang.DeleteVirtualDirectory(this.Context.Parameters["vdname"]);
                            ismang.CreateVirtualDirectory(this.Context.Parameters["vdname"], this.Context.Parameters["tdname"] + "WebService");
                        }
                        catch (Exception e)
                        {
                            throw new Exception("覆盖虚拟目录时出错!" + e.Message.ToString());
                        }
                    }
                }
                #endregion

                AddShortCut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), this.Context.Parameters["tdname"] + "ServiceManager\\Contamination Explorer Server.exe", "Contamination Explorer Server");
                AddShortCut(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\\Contamination Explorer Server", this.Context.Parameters["tdname"] + "ServiceManager\\Contamination Explorer Server.exe", "Contamination Explorer Server");
               

                #region 设置 ServiceManager 自动启动
                string m_KeyDir = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
                RegistryKey m_regLOCALMACHINE = Registry.LocalMachine;
                string m_ValueName = "Contamination ExplorerServiceManager";
                string m_ValueProperty = this.Context.Parameters["tdname"] + "ServiceManager\\Contamination Explorer Server.exe";
                AutoStart m_as = new AutoStart();
                if (!m_as.PropertyExisted(m_regLOCALMACHINE, m_KeyDir, m_ValueProperty))
                    m_as.CreateValue(m_regLOCALMACHINE, m_KeyDir, m_ValueName, m_ValueProperty);
                #endregion

                //改写 WebConfig 中的 Licence 路径
                m_wc.SetWebConfig(WebConfig.MyCMMIConfiguration, "MyCMMI2007.MyCMMI2007.LicencePath", this.Context.Parameters["tdname"] + "ServiceManager\\Contamination Explorer.lic");

                GetDiskSerial();

                MessageBox.Show("安装结束后请重新启动计算机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
                MessageBox.Show(ex.Message.ToString());
            }
		}

		public override void Uninstall(IDictionary mySavedState)
		{
			base.Uninstall(mySavedState);
		}

		private bool AddShortCut(string locationfolder,string targetDir,string linkName)
		{
			try
			{
				if(!Directory.Exists(locationfolder)) 
				{
					DirectoryInfo di = Directory.CreateDirectory(locationfolder);
				}
				string folder = locationfolder + "\\" + linkName;
				// Create a Windows Script Host Shell class
				IWshShell_Class shell = new IWshShell_ClassClass();
				// Define the shortcut file
				IWshShortcut_Class shortcut = shell.CreateShortcut(folder + ".lnk") as IWshShortcut_Class;
				shortcut.TargetPath = targetDir;
				shortcut.Save();   
				return true;  
			}
			catch
			{
				return false;
			}      
		}

		#region GetDiskSerial

		private void GetDiskSerial()
		{
			try
			{	//安装后弹出校验码的提示文本,以便用户进行注册获取许可证文件//wuxiangrong 2005.10.26			
				String infos = @"请使用该校验码:" + GetDiskID() + @",到相应的网站上注册,获取许可证文件!"; 
				FileStream fs = new FileStream("ReadMe.txt", FileMode.OpenOrCreate);
				StreamWriter sw = new StreamWriter(fs);
				sw.Write(infos);
				sw.Close();
				Process.Start("ReadMe.txt");

				//MessageBox.Show("安装结束后请重新启动计算机","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}

		// xfpan [5/17/2004]
		/// <summary>
		/// 获取机器唯一标志
		/// </summary>
		/// <returns>机器唯一标志</returns>
		private string GetDiskID()
		{
			string DiskID = "";

			DiskID = GetMAC().Trim();
            if (!String.IsNullOrEmpty(DiskID))
			{
				DiskID = "MA-" + DiskID;
				return DiskID;
			}

			try
			{
				DiskID = GetDiskSerialNo().Trim();
                if (!String.IsNullOrEmpty(DiskID))
				{
					DiskID = "HD" + DiskID;
					return DiskID;
				}
			}
			catch{}			

			DiskID = GetCPUId();
            if (!String.IsNullOrEmpty(DiskID))
            {
                DiskID = "CP" + DiskID;
                return DiskID;
            }

			return "";
		}	
				
		private string GetCPUId()
		{
			String HDid = "";
			ManagementClass cimobject = new ManagementClass("Win32_Processor");
			try
			{
				ManagementObjectCollection moc = cimobject.GetInstances();
				foreach(ManagementObject mo in moc)
				{
					HDid = mo["UniqueId"].ToString();
                    if (!String.IsNullOrEmpty(HDid))
                        break;
				}
			}
			catch
			{
				HDid = "";
			}
			return HDid.Trim();
		}

		private string GetMAC()
		{
			string strMac = "";
			try
			{
				strMac = GetMacAddr();
			}
			catch
			{
				strMac = "";
			}
			return strMac.Trim();
		}
		// xfpan [5/17/2004]
		#endregion
				
    }
}