首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#如何启动和停止windows服务

2011-12-17 
c#怎么启动和停止windows服务怎么用c#启动一个windows服务,比如说alerter,Server之类的谢谢![解决办法]调

c#怎么启动和停止windows服务
怎么用c#启动一个windows服务,比如说alerter,   Server之类的
谢谢!

[解决办法]
调用CMD命令,Net /stop Net /start
[解决办法]
类似这样的(下面是实现Ping的一个类,得到它的Ping time):

//*********************************************************************************************************
//
// Revision log:
// DateInitialsDescription
// 2007/02/12joe.guoPing command
//
// Copyright (c) 2006 Flex-Logic Limited. All Rights Reserved.
//*********************************************************************************************************
using System;
using System.Diagnostics;

namespace FLogic.Workflow.Utility.SystemMonitor {

/// <summary>
/// Ping object
/// </summary>
public class PingObject {

/// <summary>
/// Ping command
/// </summary>
/// <param name= "strIp "> a Ip address or domain name, (like: "127.0.0.1 " or "www.sohu.com " and computer name </param>
/// <returns> pingtime or other status info. </returns>
public static string CmdPing(string strIp) {
try {
Process p = new Process();

// call cmd.exe
p.StartInfo.FileName = "cmd.exe ";

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;

// not display window
p.StartInfo.CreateNoWindow = true;

p.Start();

p.StandardInput.WriteLine ( "ping -n 1 "+strIp);

p.StandardInput.WriteLine( "exit ");

string strRst = p.StandardOutput.ReadToEnd();

string pingrst;
int averageLocation = strRst.IndexOf( "Average = ");
// get ping time
if(averageLocation !=-1) {
int endLocation = strRst.IndexOf( '\r ',averageLocation);
pingrst = strRst.Substring(averageLocation + 10,endLocation - averageLocation - 10);

// timeout
} else if(strRst.IndexOf( "(100% loss) ")!=-1) {
pingrst = "Timeout ";

// Destination host unreachable
} else if( strRst.IndexOf( "Destination host unreachable. ")!=-1) {
pingrst = "Destination host unreachable ";

// Unknown host
} else if(strRst.IndexOf( "Unknown host ")!=-1) {
pingrst = "Unknown host ";

// others
} else {
pingrst = strRst;

}

p.Close();

return pingrst;

} catch (Exception ex) {
throw(new Exception( "Ping " + strIp + " occur error ",ex));
}
}

}

}
[解决办法]
下面的示例使用 ServiceController 类检查 Telnet 服务的当前状态。如果该服务已停止,此示例将启动该服务。如果该服务正在运行,此示例将停止该服务。

// 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());
[解决办法]
//dos命令的调用方法
Process p = new Process();
p.StartInfo.FileName = "cmd.exe ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;

string pingrst;

p.Start();
p.StandardInput.WriteLine( "dir d:\\ ");

p.StandardInput.WriteLine( "exit ");

string strRst = p.StandardOutput.ReadToEnd();

p.Close();

textBox1.Text = strRst;

[解决办法]
示例 -- 启动Oralce Linstener服务

// OracleLinstenerStart.cs
// Author by Yzl

using System;
using System.Management;

public class OracleLinstenerStart
{
public static void Main(string[] args)
{
ManagementObject OracleLinstenerObject = new ManagementObject(@ "root\cimv2 ",
"Win32_Service.Name= 'OracleoracleTNSListener ' ",null);
ManagementBaseObject outObject = OracleLinstenerObject.InvokeMethod( "StartService ",null,null);

int iRet = int.Parse(outObject[ "returnValue "].ToString());
switch(iRet)
{
case 0:Console.WriteLine( "Oralce Linstener服务正常启动. ");break;
case 10:Console.WriteLine( "Oralce Linstener服务已经启动. ");break;
default:
Console.WriteLine( "其他原因,请参照MSDN ");break;
}
}
}

你可以将Win32_Service.Name= 'OracleoracleTNSListener ' 改为Win32_Service.Name= 'Telnet '

同时注意Telnet是否被禁用掉了,然后被禁用则必须先调用Win32_Service的Change方法,将它启用即可.

参阅:http://blog.csdn.net/yuzl32/archive/2007/03/12/1527384.aspx
[解决办法]
停止服务> > > > ServiceController类


ServiceController[] sers;
sers = ServiceController.GetServices(Dns.GetHostName());
ServiceController ser;
for (int i = 0; i < sers.Length; i++)
{
ser = sers[i];
if (sers[i].ServiceName.ToString() == listView1.SelectedItems[0].Text.ToString())
{
ser.Stop();//ser.Pause();暂停 ser.Start();启动
}
}


最好写的时候加个判断``判断一下服务的当前状态``

热点排行