谢谢!
63 个解决方案
#1
MARK先,我也想知道答案~~~
VB.NET 2003的
VB.NET 2003的
#2
我用C#2005,很急,希望大家帮帮,非常感谢!
#3
调用CMD命令,Net /stop Net /start
#4
用c#代码怎么写?
#5
类似这样的(下面是实现Ping的一个类,得到它的Ping time):
//*********************************************************************************************************
//
// Revision log:
// Date Initials Description
// 2007/02/12 joe.guo Ping 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));
}
}
}
}
//*********************************************************************************************************
//
// Revision log:
// Date Initials Description
// 2007/02/12 joe.guo Ping 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));
}
}
}
}
#6
这样知道了吧? p.StandardInput.WriteLine ("ping -n 1 "+strIp); 把这里换一下:
p.StandardInput.WriteLine ("Net Stop " + yourServiceName);
p.StandardInput.WriteLine ("Net Stop " + yourServiceName);
#7
下面的示例使用 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());
// 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());
#8
ServiceController.Status.Equals(ServiceControllerStatus.Stopped)
判定状态
判定状态
#9
非常感谢 我先试验一下
#10
Process 类
#11
如果我要控制别人机器上的服务该怎么办,如果有对方机器的远程登录用户名和密码
#12
比如打开sql服务
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = "MSSQLSERVER";
if( sc.Status != ServiceControllerStatus.Running )
sc.Start();
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = "MSSQLSERVER";
if( sc.Status != ServiceControllerStatus.Running )
sc.Start();
#13
http://book.csdn.net/bookfiles/16/100165809.shtml
#14
可以在构造函数中指定机器的名字:
ServiceController sc = new ServiceController(MachingName, "Telnet");
ServiceController sc = new ServiceController(MachingName, "Telnet");
#15
也可以用WMI
不过我推荐用 Net Stop 和Net Start
操作简单也方便,如果你用操作系统的计划任务就能满足要求的话,把上面的CMD命令写在 bat文件里就都OK了
不过我推荐用 Net Stop 和Net Start
操作简单也方便,如果你用操作系统的计划任务就能满足要求的话,把上面的CMD命令写在 bat文件里就都OK了
#16
//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;
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;
#17
如果我要控制别人机器上的服务该怎么办,如果有对方机器的远程登录用户名和密码
====================================
请使用wmi
====================================
请使用wmi
#18
示例 -- 启动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
// 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
#19
用ServiceController类就行了,方法都很好用
只要注意几点
1.服务的关闭与开启都是需要很长时间的,需要判断状态
2.不是所有的服务都可以进行关闭,开启,暂停操作的.
只要注意几点
1.服务的关闭与开启都是需要很长时间的,需要判断状态
2.不是所有的服务都可以进行关闭,开启,暂停操作的.
#20
一陀陀的强人
mark一个先
mark一个先
#21
mark
#22
affas
#23
用ServiceController控制服务的启动、关闭
#24
mark
#25
mark
#26
mark!
#27
up
#28
mark
#29
mark!!!
#30
也留個記號~
#31
顶顶顶
#32
mark
#33
这样的话,用C#编远程入侵程序很简单了哦....
现在中国的牛人这么多,相信中国的软件业在不久的将来一定会飞黄腾达地....
现在中国的牛人这么多,相信中国的软件业在不久的将来一定会飞黄腾达地....
#34
up,
#35
mark
#36
up
#37
用ServiceController类
#38
up者有分
#39
停止服务>>>>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();启动
}
}
最好写的时候加个判断``判断一下服务的当前状态``
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();启动
}
}
最好写的时候加个判断``判断一下服务的当前状态``
#40
mark
#41
protected void btnRestart_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("wuauserv");
sc.ServiceName = "localhost";
switch (sc.Status)
{
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Refresh();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Running:
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
default:
sc.Start();
break;
}
}
}
{
ServiceController sc = new ServiceController("wuauserv");
sc.ServiceName = "localhost";
switch (sc.Status)
{
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Refresh();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Running:
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
default:
sc.Start();
break;
}
}
}
#42
见:http://blog.csdn.net/zhoufoxcn/archive/2007/03/14/1528824.aspx
在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称,这里以IIS检测为例,我们知道IIS的显示名称是"IIS Admin",现在用下面的代码来判断IIS是否启动。
ServiceController[] service=ServiceController.GetServices();
bool isStart = false;
for (int i = 0; i < service.Length; i++)
...{
if (service[i].DisplayName.ToUpper().Equals("IIS Admin".ToUpper()))
...{
if (service[i].Status == ServiceControllerStatus.Running)
...{
isStart = true;
break;
}
}
}
if (isStart)
...{
MessageBox.Show("服务已经启动");
}
else
...{
MessageBox.Show("服务没启动");
}
在使用时别忘记导入System.ServiceProcess这个名称空间,一般情况下VS.net是不会自动导入的。
获取了ServiceController 实例之后就可以Stop()/Pause ()/Start ()了。
在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称,这里以IIS检测为例,我们知道IIS的显示名称是"IIS Admin",现在用下面的代码来判断IIS是否启动。
ServiceController[] service=ServiceController.GetServices();
bool isStart = false;
for (int i = 0; i < service.Length; i++)
...{
if (service[i].DisplayName.ToUpper().Equals("IIS Admin".ToUpper()))
...{
if (service[i].Status == ServiceControllerStatus.Running)
...{
isStart = true;
break;
}
}
}
if (isStart)
...{
MessageBox.Show("服务已经启动");
}
else
...{
MessageBox.Show("服务没启动");
}
在使用时别忘记导入System.ServiceProcess这个名称空间,一般情况下VS.net是不会自动导入的。
获取了ServiceController 实例之后就可以Stop()/Pause ()/Start ()了。
#43
在下也按照楼上各位的方法去试了,但是却提示错误,比如我想启动Telnet,运行时却提示:用户代码未处理InvalidOperationException 无法启动计算机“.”上的服务 Telnet.请各位指教一下,谢谢!!
#44
确实说的很不错。Mark下
#45
确实说的很不错。Mark下
#46
收藏一下先!
#47
好东西,记录下...
#48
很强,学习一下。。
#49
学习
#50
学习
#1
MARK先,我也想知道答案~~~
VB.NET 2003的
VB.NET 2003的
#2
我用C#2005,很急,希望大家帮帮,非常感谢!
#3
调用CMD命令,Net /stop Net /start
#4
用c#代码怎么写?
#5
类似这样的(下面是实现Ping的一个类,得到它的Ping time):
//*********************************************************************************************************
//
// Revision log:
// Date Initials Description
// 2007/02/12 joe.guo Ping 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));
}
}
}
}
//*********************************************************************************************************
//
// Revision log:
// Date Initials Description
// 2007/02/12 joe.guo Ping 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));
}
}
}
}
#6
这样知道了吧? p.StandardInput.WriteLine ("ping -n 1 "+strIp); 把这里换一下:
p.StandardInput.WriteLine ("Net Stop " + yourServiceName);
p.StandardInput.WriteLine ("Net Stop " + yourServiceName);
#7
下面的示例使用 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());
// 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());
#8
ServiceController.Status.Equals(ServiceControllerStatus.Stopped)
判定状态
判定状态
#9
非常感谢 我先试验一下
#10
Process 类
#11
如果我要控制别人机器上的服务该怎么办,如果有对方机器的远程登录用户名和密码
#12
比如打开sql服务
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = "MSSQLSERVER";
if( sc.Status != ServiceControllerStatus.Running )
sc.Start();
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = "MSSQLSERVER";
if( sc.Status != ServiceControllerStatus.Running )
sc.Start();
#13
http://book.csdn.net/bookfiles/16/100165809.shtml
#14
可以在构造函数中指定机器的名字:
ServiceController sc = new ServiceController(MachingName, "Telnet");
ServiceController sc = new ServiceController(MachingName, "Telnet");
#15
也可以用WMI
不过我推荐用 Net Stop 和Net Start
操作简单也方便,如果你用操作系统的计划任务就能满足要求的话,把上面的CMD命令写在 bat文件里就都OK了
不过我推荐用 Net Stop 和Net Start
操作简单也方便,如果你用操作系统的计划任务就能满足要求的话,把上面的CMD命令写在 bat文件里就都OK了
#16
//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;
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;
#17
如果我要控制别人机器上的服务该怎么办,如果有对方机器的远程登录用户名和密码
====================================
请使用wmi
====================================
请使用wmi
#18
示例 -- 启动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
// 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
#19
用ServiceController类就行了,方法都很好用
只要注意几点
1.服务的关闭与开启都是需要很长时间的,需要判断状态
2.不是所有的服务都可以进行关闭,开启,暂停操作的.
只要注意几点
1.服务的关闭与开启都是需要很长时间的,需要判断状态
2.不是所有的服务都可以进行关闭,开启,暂停操作的.
#20
一陀陀的强人
mark一个先
mark一个先
#21
mark
#22
affas
#23
用ServiceController控制服务的启动、关闭
#24
mark
#25
mark
#26
mark!
#27
up
#28
mark
#29
mark!!!
#30
也留個記號~
#31
顶顶顶
#32
mark
#33
这样的话,用C#编远程入侵程序很简单了哦....
现在中国的牛人这么多,相信中国的软件业在不久的将来一定会飞黄腾达地....
现在中国的牛人这么多,相信中国的软件业在不久的将来一定会飞黄腾达地....
#34
up,
#35
mark
#36
up
#37
用ServiceController类
#38
up者有分
#39
停止服务>>>>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();启动
}
}
最好写的时候加个判断``判断一下服务的当前状态``
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();启动
}
}
最好写的时候加个判断``判断一下服务的当前状态``
#40
mark
#41
protected void btnRestart_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("wuauserv");
sc.ServiceName = "localhost";
switch (sc.Status)
{
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Refresh();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Running:
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
default:
sc.Start();
break;
}
}
}
{
ServiceController sc = new ServiceController("wuauserv");
sc.ServiceName = "localhost";
switch (sc.Status)
{
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Refresh();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Running:
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
sc.Start();
break;
default:
sc.Start();
break;
}
}
}
#42
见:http://blog.csdn.net/zhoufoxcn/archive/2007/03/14/1528824.aspx
在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称,这里以IIS检测为例,我们知道IIS的显示名称是"IIS Admin",现在用下面的代码来判断IIS是否启动。
ServiceController[] service=ServiceController.GetServices();
bool isStart = false;
for (int i = 0; i < service.Length; i++)
...{
if (service[i].DisplayName.ToUpper().Equals("IIS Admin".ToUpper()))
...{
if (service[i].Status == ServiceControllerStatus.Running)
...{
isStart = true;
break;
}
}
}
if (isStart)
...{
MessageBox.Show("服务已经启动");
}
else
...{
MessageBox.Show("服务没启动");
}
在使用时别忘记导入System.ServiceProcess这个名称空间,一般情况下VS.net是不会自动导入的。
获取了ServiceController 实例之后就可以Stop()/Pause ()/Start ()了。
在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称,这里以IIS检测为例,我们知道IIS的显示名称是"IIS Admin",现在用下面的代码来判断IIS是否启动。
ServiceController[] service=ServiceController.GetServices();
bool isStart = false;
for (int i = 0; i < service.Length; i++)
...{
if (service[i].DisplayName.ToUpper().Equals("IIS Admin".ToUpper()))
...{
if (service[i].Status == ServiceControllerStatus.Running)
...{
isStart = true;
break;
}
}
}
if (isStart)
...{
MessageBox.Show("服务已经启动");
}
else
...{
MessageBox.Show("服务没启动");
}
在使用时别忘记导入System.ServiceProcess这个名称空间,一般情况下VS.net是不会自动导入的。
获取了ServiceController 实例之后就可以Stop()/Pause ()/Start ()了。
#43
在下也按照楼上各位的方法去试了,但是却提示错误,比如我想启动Telnet,运行时却提示:用户代码未处理InvalidOperationException 无法启动计算机“.”上的服务 Telnet.请各位指教一下,谢谢!!
#44
确实说的很不错。Mark下
#45
确实说的很不错。Mark下
#46
收藏一下先!
#47
好东西,记录下...
#48
很强,学习一下。。
#49
学习
#50
学习