1   public class ContractManager
  2     {
  3 
  4         IDictionary stateSaver = new Hashtable();
  5         /// <summary>  
  6         /// 安装服务  
  7         /// </summary>  
  8         /// <param name="p_Path">指定服务文件路径</param>  
  9         /// <param name="p_ServiceName">返回安装完成后的服务名</param>  
 10         /// <returns>安装信息 正确安装返回""</returns>  
 11         public static string InsertService(string p_Path, ref string p_ServiceName)
 12         {
 13             if (!File.Exists(p_Path)) return "文件不存在!";
 14             p_ServiceName = "";
 15             FileInfo _InsertFile = new FileInfo(p_Path);
 16             IDictionary _SavedState = new Hashtable();
 17             try
 18             {
 19                 //加载一个程序集,并运行其中的所有安装程序。  
 20                 AssemblyInstaller _AssemblyInstaller = new AssemblyInstaller(p_Path, new string[] { "/LogFile=" + _InsertFile.DirectoryName + "\\" + _InsertFile.Name.Substring(0, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" });
 21                 _AssemblyInstaller.UseNewContext = true;
 22                 _AssemblyInstaller.Install(_SavedState);
 23                 _AssemblyInstaller.Commit(_SavedState);
 24                 Type[] _TypeList = _AssemblyInstaller.Assembly.GetTypes();//获取安装程序集类型集合  
 25                 for (int i = 0; i != _TypeList.Length; i++)
 26                 {
 27                     if (_TypeList[i].BaseType.FullName == "System.Configuration.Install.Installer")
 28                     {
 29                         //找到System.Configuration.Install.Installer 类型  
 30                         object _InsertObject = System.Activator.CreateInstance(_TypeList[i]);//创建类型实列  
 31                         FieldInfo[] _FieldList = _TypeList[i].GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
 32                         for (int z = 0; z != _FieldList.Length; z++)
 33                         {
 34                             if (_FieldList[z].FieldType.FullName == "System.ServiceProcess.ServiceInstaller")
 35                             {
 36                                 object _ServiceInsert = _FieldList[z].GetValue(_InsertObject);
 37                                 if (_ServiceInsert != null)
 38                                 {
 39                                     p_ServiceName = ((ServiceInstaller)_ServiceInsert).ServiceName;
 40                                     return "";
 41                                 }
 42                             }
 43                         }
 44                     }
 45                 }
 46                 return "";
 47             }
 48             catch (Exception ex)
 49             {
 50                 return ex.Message;
 51             }
 52         }
 53         /// <summary>
 54         /// 卸载服务
 55         /// </summary>
 56         /// <param name="filepath">服务路径</param>
 57         public static void UnInstallService(string filepath, string ServiceName)
 58         {
 59 
 60             try
 61             {
 62 
 63                 if (ServiceIsExisted(ServiceName))
 64                 {
 65 
 66                     //UnInstall Service  
 67 
 68                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
 69 
 70                     myAssemblyInstaller.UseNewContext = true;
 71 
 72                     myAssemblyInstaller.Path = filepath;
 73 
 74                     myAssemblyInstaller.Uninstall(null);
 75 
 76                     myAssemblyInstaller.Dispose();
 77 
 78                 }
 79 
 80             }
 81 
 82             catch (Exception ex)
 83             {
 84                 throw new Exception("unInstallServiceError\n" + ex.Message);
 85 
 86             }
 87 
 88         }
 89         /// <summary>
 90         /// 判断服务是否存在
 91         /// </summary>
 92         /// <param name="serviceName">服务名</param>
 93         /// <returns></returns>
 94         public static bool ServiceIsExisted(string serviceName)
 95         {
 96             ServiceController[] services = ServiceController.GetServices();
 97             foreach (ServiceController s in services)
 98             {
 99                 if (s.ServiceName == serviceName)
100                 {
101                     return true;
102                 }
103             }
104             return false;
105         }
106         /// <summary>  
107         /// 启动服务  
108         /// </summary>  
109         /// <param name="serviceName"></param>  
110         /// <returns></returns>  
111         public static bool ServiceStart(string serviceName)
112         {
113             try
114             {
115                 ServiceController service = new ServiceController(serviceName);
116                 if (service.Status == ServiceControllerStatus.Running)
117                 {
118                     return true;
119                 }
120                 else
121                 {
122                     TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
123                     service.Start();
124                     service.WaitForStatus(ServiceControllerStatus.Running, timeout);
125                 }
126             }
127             catch
128             {
129                 return false;
130             }
131             return true;
132         }
133 
134         /// <summary>  
135         /// 停止服务  
136         /// </summary>  
137         /// <param name="serviseName"></param>  
138         /// <returns></returns>  
139         public static bool ServiceStop(string serviseName)
140         {
141             try
142             {
143                 ServiceController service = new ServiceController(serviseName);
144                 if (service.Status == ServiceControllerStatus.Stopped)
145                 {
146                     return true;
147                 }
148                 else
149                 {
150                     TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
151                     service.Stop();
152                     service.WaitForStatus(ServiceControllerStatus.Running, timeout);
153                 }
154             }
155             catch
156             {
157                 return false;
158             }
159             return true;
160         }
161        
162     }

 

posted on 2018-01-19 09:46  高兴happy  阅读(156)  评论(0编辑  收藏  举报