Breathe李

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  自从发了上次的博文之后,就一直在出差,由于出差比较繁忙,故博客一直没有更新。在出差期间,问题比较杂乱,并没有记录很相信的步骤,只是略微记住在制作安装包的过程中,遇到到问题。

   主要问题还是说根据进程的名称,查找是否为用户使用的进程!

  全部代码如下:

View Code
1 using System;
2  using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6 using System.Diagnostics;
7 using System.IO;
8 using System.Management;
9
10
11 namespace SetupSupport
12 {
13 class Program
14 {
15 staticvoid Main(string[] args)
16 {
17
18
19 GetAllProcess();
20 //Console.Read();
21 SetupSup s =new SetupSup();
22 s.Start();
23 }
24 ///<summary>
25 /// 查找机器所有进程
26 ///</summary>
27 privatestaticvoid GetAllProcess()
28 {
29 string[] processNameArry = { "msiexec", "AMX_VOD_ACTIVEX" };
30 Process[] processAllList = Process.GetProcesses();
31 foreach (Process proc in processAllList)
32 {
33 KilledProcess(proc.Id, processNameArry);
34 }
35
36 }
37 ///<summary>
38 /// 关闭非System用户的,msiexec.exe进程
39 ///</summary>
40 ///<param name="ProcessID"></param>
41 ///<param name="hostName"></param>
42 privatestaticvoid KilledProcess(int ProcessID,string[] processName)
43 {
44 try
45 {
46 string userName = GetProcessName(ProcessID);
47 if (string.IsNullOrEmpty(userName) ==false)
48 {
49 if (!userName.ToLower().Trim().Equals("system"))
50 {
51 Process p = Process.GetProcessById(ProcessID);
52 if (p !=null)
53 {
54 for (int i =0; i < processName.Length; i++)
55 {
56 if (p.ProcessName.Equals(processName[i]))
57 {
58 p.Kill();
59 }
60 }
61 }
62 }
63 }
64
65 }
66 catch (Exception ex)
67 {
68
69 Console.WriteLine(ex.StackTrace);
70 }
71 }
72 ///<summary>
73 /// 根据进程ID查找进程,并返回进程所属的用户名
74 ///</summary>
75 ///<param name="ProcessID"></param>
76 ///<returns></returns>
77 publicstaticstring GetProcessName(int ProcessID)
78 {
79 string userName =string.Empty;
80 SelectQuery query1 =new SelectQuery("Select * from Win32_Process WHERE processID="+ ProcessID);//通过System.Management中的SelectQuery类查询进程信息
81 ManagementObjectSearcher searcher1 =new ManagementObjectSearcher(query1);
82 foreach (ManagementObject item in searcher1.Get())
83 {
84 ManagementBaseObject inPar =null;
85 ManagementBaseObject outPar =null;
86
87 inPar = item.GetMethodParameters("GetOwner");
88
89 outPar = item.InvokeMethod("GetOwner", inPar, null);
90
91 try
92 {
93 userName = outPar["User"].ToString();//返回非系统进程的用户名称
94
95 }
96 catch
97 {
98
99 userName ="system";
100 }
101 }
102 return userName;
103 }
104 }
105 publicclass SetupSup
106 {
107 int iCount =-1;//记录是否为第一次
108 System.Threading.Thread threadSetup =null;
109 public SetupSup()
110 {
111
112 }
113
114 privatebool CheckProcess(refstring sAppName)
115 {
116 string userName =string.Empty;
117 bool isExist =false;
118 string processName =string.Empty;
119 if (sAppName.Equals("GisActiveOcx2.3.msi")||sAppName.Equals("SISS_Setup.msi"))//如果文件名为Gis地图插件或者为自己制作的安装程序
120 {
121 processName ="msiexec";//进程名为msiexec
122 }
123 else
124 {
125 processName ="AMX_VOD_ACTIVEX";//视频进程的名称
126 }
127 Process[] processByNameArr = Process.GetProcessesByName(processName);//根据名称查找进程
128 if (processByNameArr.Length<=0)
129 {
130 isExist =false;
131 }
132 foreach (Process proce in processByNameArr)
133 {
134 userName = Program.GetProcessName(proce.Id);
135 if (string.IsNullOrEmpty(userName))
136 {
137 continue;
138 }
139 elseif (!userName.Equals("SYSTEM"))
140 {
141 isExist =true;
142 }
143
144 }
145 //更改程序名称
146 if (iCount>=0&&isExist==false)
147 {
148 switch (sAppName)
149 {
150 case"GisActiveOcx2.3.msi":
151 sAppName ="AMX_VOD_ACTIVEX.exe";
152 break;
153 case"AMX_VOD_ACTIVEX.exe":
154 sAppName ="SISS.msi";
155 break;
156 case"SISS.msi":
157 sAppName =string.Empty;
158 break;
159 }
160 }
161 return isExist;
162
163 }
164 publicvoid Start()
165 {
166 threadSetup =new Thread(new ThreadStart(Setup));
167 threadSetup.Start();
168 }
169 privatevoid Setup()
170 {
171 string sAppName ="GisActiveOcx2.3.msi";//当前执行的exe 名称
172
173 while (true)
174 {
175 try
176 {
177 //解压前安装 3.5框架
178 //Console.Write(".");
179 if (iCount >=0)
180 {
181 if (CheckProcess(ref sAppName))
182 {
183 System.Threading.Thread.Sleep(500);
184 continue;
185 }
186 else
187 {
188 if (string.IsNullOrEmpty(sAppName))
189 {
190 Process p = Process.GetCurrentProcess();
191 p.Kill();
192 }
193 else
194 {
195 ProcessStartInfo psi =new ProcessStartInfo();
196 psi.FileName = Directory.GetCurrentDirectory() +"\\Data\\"+ sAppName;
197 Process.Start(psi);
198 }
199 }
200 }
201 else
202 {
203 //安装视频插件
204 //客户端
205 //安装gis插件
206 iCount++;
207 ProcessStartInfo psi =new ProcessStartInfo();
208 psi.FileName = Directory.GetCurrentDirectory() +"\\Data\\"+ sAppName;
209 Process.Start(psi);
210 }
211
212
213 //
214 }
215 catch (Exception ex)
216 {
217 Console.WriteLine("error:"+ ex.Message +"->"+ ex.StackTrace);
218 }
219 System.Threading.Thread.Sleep(500);
220 }
221 }
222 }
223 }
posted on 2011-05-16 14:53  LouisLee  阅读(1003)  评论(1编辑  收藏  举报