å
³é®ä»£ç
3.1 è·åç³»ç»æ³¨éä¿¡æ¯
æ¹æ³:éè¿ä¸Microsoft.Win32.SessionEndingEventHandler建å
³å
³èè·åç³»ç»æ³¨éä¿¡æ¯.
protected override void OnStart(string[] args)
{
//è·åç³»ç»æ³¨é,å
³æº
Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(this.SystemEvents_SessionEnding);
}
protected override void OnStop()
{
// TODO: å¨æ¤å¤æ·»å 代ç 以æ§è¡åæ¢æå¡æéçå
³éæä½ã
//åè®°å¨æå¡åæ¢æ¶,移é¤äºä»¶.
Microsoft.Win32.SystemEvents.SessionEnding -= new Microsoft.Win32.SessionEndingEventHandler(this.SystemEvents_SessionEnding);
}
3.2 æ£æ¥å¹¶å¯å¨Windowsæå¡
/// <summary>
/// å¯å¨å·²æåæåæ¢çæå¡
/// </summary>
private void StartService()
{
try
{
foreach (string serviceName in rwCnfg.GsServiceNames)
{
ServiceController myService = new ServiceController(serviceName);
ServiceControllerStatus status = myService.Status;
switch (status)
{
case ServiceControllerStatus.ContinuePending:
break;
case ServiceControllerStatus.PausePending:
break;
case ServiceControllerStatus.StartPending:
break;
case ServiceControllerStatus.Running:
break;
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.Stopped:
{
myService.Start();
myService.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 2, 0));
Common.wLog("å®æå¯å¨æå¡: " + myService.ServiceName + " . " + System.DateTime.Now.ToString());
}
break;
case ServiceControllerStatus.StopPending:
break;
default:
break;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
3.2 æ£æ¥å¹¶å¯å¨å¯¹åºGUIç¨åº
/// <summary>
/// å¯å¨ææè¦å¯å¨çç¨åº
/// </summary>
private void StartProgram()
{
try
{
foreach (string ProgramPath in rwCnfg.GsProgramPaths)
{
string fileName = "";
//fileName = System.IO.Path.GetFileName(ProgramPath); //æ件å
//string ext = System.IO.Path.GetExtension(ProgramPath); //æ©å±å
fileName = System.IO.Path.GetFileNameWithoutExtension(ProgramPath);// fileName.Replace(ext, "");
if (!IsExistProcess(fileName))
{
ProcessStartInfo startInfo = new ProcessStartInfo(ProgramPath);
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
Common.wLog("å®æå¯å¨ç¨åº: " + fileName + ",å®æ´è·¯å¾:" + ProgramPath + " . " + System.DateTime.Now.ToString());
System.Threading.Thread.Sleep(3 * 1000); //é´é3ç§;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
/// <summary>
/// æ£æ¥è¯¥è¿ç¨æ¯å¦å·²å¯å¨
/// </summary>
/// <param name="processName"></param>
/// <returns></returns>
private bool IsExistProcess(string processName)
{
Process[] MyProcesses = Process.GetProcesses();
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName.CompareTo(processName) == 0)
{
return true;
}
}
return false;
}
3.3 为å½åWindowsæå¡è®¾ç½®å¯ä¸æ¡é¢äº¤äºé项
为"serviceProcessInstaller1" ç Committed äºä»¶æ·»å 以ä¸æä½:
(注æå¼å ¥ System.Management å½å空é´)
private void serviceProcessInstaller1_Committed(object sender, InstallEventArgs e)
{
try
{
ConnectionOptions myConOptions = new ConnectionOptions();
myConOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", myConOptions);
mgmtScope.Connect();
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
æ§è¡ææ: (ä¸åéè¦æå¨å»è®¾ç½®)