[C#]-システムをシャットダウンするサービスを開く
30954 ワード
ソース:http://hi.baidu.com/%B0%D9%CD%F5%C5%F3%B6%C8/blog/item/221c015acf754e8d810a1813.html
本例ではTelnetサービスをテストします
*********************************************************************************************************************
ソース:http://book.csdn.net/bookfiles/16/100165809.shtml
書名:C#プレミアムプログラミング(第3版) 作者:(美)ロビンソン&ネゲル著、李敏波訳 出所:清華大学出版社 ISBN:7-302-10199-X 定価:128元 この例のテスト対象はすべてのシステムサービスです
本例ではTelnetサービスをテストします
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
//
System.ServiceProcess.dll
using
System.ServiceProcess;
namespace
WindowsFormsApplication1
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//
Toggle the Telnet service:
//
If it is started (running, paused, etc), stop the service.
//
If it is stopped, start the service.
ServiceController sc
=
new
ServiceController(
"
Telnet
"
);
Console.WriteLine(
"
The Telnet service status is currently set to {0}
"
, sc.Status.ToString());
if
((sc.Status.Equals(ServiceControllerStatus.Stopped))
||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
//
Start the service if the current status is stopped.
Console.WriteLine(
"
Starting the Telnet service...
"
);
sc.Start();
}
else
{
//
Stop the service if its status is not set to "Stopped ".
Console.WriteLine(
"
Stopping the Telnet service...
"
);
sc.Stop();
}
//
Refresh and display the current service status.
sc.Refresh();
Console.WriteLine(
"
The Telnet service status is now set to {0}.
"
, sc.Status.ToString());
}
}
}
*********************************************************************************************************************
ソース:http://book.csdn.net/bookfiles/16/100165809.shtml
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
//
System.ServiceProcess.dll
using
System.ServiceProcess;
namespace
WindowsFormsApplication1
{
public
partial
class
ServiceControlForm : Form
{
private
System.ServiceProcess.ServiceController[] services;
public
ServiceControlForm()
{
//
//
Required for Windows Form Designer support
//
InitializeComponent();
RefreshServiceList();
}
protected
void
RefreshServiceList()
{
services
=
ServiceController.GetServices();
listBoxServices.DisplayMember
=
"
DisplayName
"
;
listBoxServices.DataSource
=
services;
}
protected
string
GetServiceTypeName(ServiceType type)
{
string
serviceType
=
""
;
if
((type
&
ServiceType.InteractiveProcess)
!=
0
)
{
serviceType
=
"
Interactive
"
;
type
-=
ServiceType.InteractiveProcess;
}
switch
(type)
{
case
ServiceType.Adapter:
serviceType
+=
"
Adapter
"
;
break
;
case
ServiceType.FileSystemDriver:
case
ServiceType.KernelDriver:
case
ServiceType.RecognizerDriver:
serviceType
+=
"
Driver
"
;
break
;
case
ServiceType.Win32OwnProcess:
serviceType
+=
"
Win32 Service Process
"
;
break
;
case
ServiceType.Win32ShareProcess:
serviceType
+=
"
Win32 Shared Process
"
;
break
;
default
:
serviceType
+=
"
unknown type
"
+
type.ToString();
break
;
}
return
serviceType;
}
protected
void
SetServiceStatus(ServiceController controller)
{
buttonStart.Enabled
=
true
;
buttonStop.Enabled
=
true
;
buttonPause.Enabled
=
true
;
buttonContinue.Enabled
=
true
;
if
(
!
controller.CanPauseAndContinue)
{
buttonPause.Enabled
=
false
;
buttonContinue.Enabled
=
false
;
}
if
(
!
controller.CanStop)
{
buttonStop.Enabled
=
false
;
}
ServiceControllerStatus status
=
controller.Status;
switch
(status)
{
case
ServiceControllerStatus.ContinuePending:
textBoxServiceStatus.Text
=
"
Continue Pending
"
;
buttonContinue.Enabled
=
false
;
break
;
case
ServiceControllerStatus.Paused:
textBoxServiceStatus.Text
=
"
Paused
"
;
buttonPause.Enabled
=
false
;
buttonStart.Enabled
=
false
;
break
;
case
ServiceControllerStatus.PausePending:
textBoxServiceStatus.Text
=
"
Pause Pending
"
;
buttonPause.Enabled
=
false
;
buttonStart.Enabled
=
false
;
break
;
case
ServiceControllerStatus.StartPending:
textBoxServiceStatus.Text
=
"
Start Pending
"
;
buttonStart.Enabled
=
false
;
break
;
case
ServiceControllerStatus.Running:
textBoxServiceStatus.Text
=
"
Running
"
;
buttonStart.Enabled
=
false
;
buttonContinue.Enabled
=
false
;
break
;
case
ServiceControllerStatus.Stopped:
textBoxServiceStatus.Text
=
"
Stopped
"
;
buttonStop.Enabled
=
false
;
break
;
case
ServiceControllerStatus.StopPending:
textBoxServiceStatus.Text
=
"
Stop Pending
"
;
buttonStop.Enabled
=
false
;
break
;
default
:
textBoxServiceStatus.Text
=
"
Unknown status
"
;
break
;
}
}
//
listBoxServices SelectedIndexChanged
protected
void
OnSelectedIndexChanged(
object
sender, System.EventArgs e)
{
ServiceController controller
=
(ServiceController)listBoxServices.SelectedItem;
textBoxDisplayName.Text
=
controller.DisplayName;
textBoxServiceType.Text
=
GetServiceTypeName(controller.ServiceType);
textBoxServiceName.Text
=
controller.ServiceName;
SetServiceStatus(controller);
}
//
buttonStart,buttonStop,buttonPause,buttonContinue Click
protected
void
buttonCommand_Click(
object
sender, System.EventArgs e)
{
Cursor.Current
=
Cursors.WaitCursor;
ServiceController controller
=
(ServiceController)listBoxServices.SelectedItem;
if
(sender
==
this
.buttonStart)
{
controller.Start();
controller.WaitForStatus(ServiceControllerStatus.Running);
}
else
if
(sender
==
this
.buttonStop)
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
}
else
if
(sender
==
this
.buttonPause)
{
controller.Pause();
controller.WaitForStatus(ServiceControllerStatus.Paused);
}
else
if
(sender
==
this
.buttonContinue)
{
controller.Continue();
controller.WaitForStatus(ServiceControllerStatus.Running);
}
int
index
=
listBoxServices.SelectedIndex;
RefreshServiceList();
listBoxServices.SelectedIndex
=
index;
Cursor.Current
=
Cursors.Default;
}
//
buttonRefresh Click
protected
void
buttonRefresh_Click(
object
sender, System.EventArgs e)
{
RefreshServiceList();
}
//
buttonExit Click
protected
void
buttonExit_Click(
object
sender, System.EventArgs e)
{
Application.Exit();
}
}
}