在.net下可以编程远程启动服务吗?
最好有代码或资料,谢谢。 问题点数:50、回复次数:13Top
1 楼lostowner()回复于 2006-03-08 17:03:43 得分 5
可以,先用net命令登陆远程计算机,然后启动服务就可以了Top
2 楼Llovenetboy(小小程序员)回复于 2006-03-08 17:48:10 得分 0
是这样,我用C#编写一个web程序,执行后可以启动其他机器的服务。例如 IIS,sql server,可以吗?Top
3 楼Raullee(项少龙)回复于 2006-03-08 18:13:11 得分 5
理论上是可以的.帮你顶一下!Top
4 楼Samen168(Code to coding)回复于 2006-03-08 18:57:17 得分 5
如果你有足够权限是可以的Top
5 楼Llovenetboy(小小程序员)回复于 2006-03-09 08:44:51 得分 0
各位老大别光说可以阿,给点代码出来。我也知道可以,可怎么实现阿。Top
6 楼Qqwwee_Com(http://qqwwee.com)回复于 2006-03-09 08:58:55 得分 5
System.ServiceProcess.ServiceController这个类就是管理windows服务的。
====CSDN 小助手 V2.5 ====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
速度快;使用方便;提供源代码。
界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx
下载:http://szlawbook.com/csdnv2(有源代码 C# vs2003)
Top
7 楼MonkWang(象写情书一样写程序)回复于 2006-03-09 09:00:02 得分 5
没接触过,帮顶
顺便学习!Top
8 楼Llovenetboy(小小程序员)回复于 2006-03-09 09:19:30 得分 0
我用到了System.ServiceProcess.ServiceController,但是在myController.Stop()本地sql server服务时提示我无法打开计算机“.”上的 MSSQLSERVER 服务,我觉得有可能是权限问题。因为低级别服务可以启动,但我如何来进行权限设计?现在用的权限是administratorTop
9 楼Llovenetboy(小小程序员)回复于 2006-03-09 09:42:58 得分 0
没有人回答么??Top
10 楼lishaowensdut(小李探花)回复于 2006-03-09 10:05:56 得分 5
建议,远程建立WEB service,该服务控制你所需的服务的启动与停止。然后本地编程调用该服务就是了Top
11 楼zlz_212(ShREk)回复于 2006-03-09 10:22:30 得分 15
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());
[C++]
// 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");
if (sc)
{
Console::WriteLine("The Telnet service status is currently set to {0}",
__box(sc->Status));
if ((sc->Status == (ServiceControllerStatus::Stopped)) ||
(sc->Status == (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}.",
__box(sc->Status));
抄了个例子给你,以前写的找不到了。不知道对你有没有用Top
12 楼tk193192(奇迹花园)回复于 2006-03-09 10:29:45 得分 5
可以Top
13 楼Llovenetboy(小小程序员)回复于 2006-03-09 11:09:23 得分 0
问题解决了,利用System.Management命名空间,测试成功,现在把代码贴出来,大家看看。多谢大家了。ServiceController 的例子我用过但是权限问题没有解决。
*********************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ServiceProcess;
using System.Management;
namespace ControlServer
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private string strPath;
private ManagementClass managementClass;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if( RemoteConnectValidate("****,****,****,****","***","***") )
{
Win32ServiceManager("****,****,****,****","***","***");
// IP, 用户名 密码
string [,] services = GetServiceList();
for(int i= 0;i<services.GetLength(0);i++)
{
Response.Write(services[i,0]);
Response.Write(services[i,1]);
Response.Write(services[i,2]);
Response.Write(services[i,3]);
Response.Write("<br>");
}
string strRst = null;
string serviceName = "IISADMIN";
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
mo.InvokeMethod("StartService",null);
//StartService 启动服务,PauseService 暂停服务,ResumeService 恢复服务,StopService 停止服务
}
}
// 验证是否能连接到远程计算机
public static bool RemoteConnectValidate(string host,string userName,string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
try
{
managementScope.Connect();
}
catch
{
}
return managementScope.IsConnected;
}
// 获取所连接的计算机的所有服务数据
public string [,] GetServiceList()
{
string [,] services = new string [this.managementClass.GetInstances().Count,4];
int i = 0;
foreach(ManagementObject mo in this.managementClass.GetInstances())
{
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
i++;
}
return services;
}
public void Win32ServiceManager(string host,string userName,string password)
{
this.strPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if(userName!=null&&userName.Length>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
this.managementClass.Scope = managementScope;
}
}
}
}
Top




