2012-01-05 32 views
0

我试图从ASP .Net Web应用程序(请注意,Web应用程序与进程位于同一台服务器上)在远程服务器上执行进程。我运行的代码如下:使用ASP .Net管理本地计算机上的进程

ConnectionOptions conO = new ConnectionOptions(); 
      // conO.Username = txtUser.Text; 
      // conO.Password = txtPassword.Text; 

      ManagementPath path = new ManagementPath(@"\\" + txtRemoteComputer.Text + @"\root\cimv2"); 

      System.Management.ManagementScope oMs = new System.Management.ManagementScope(path, conO); 
      oMs.Connect(); 

      ObjectGetOptions opt = new ObjectGetOptions(); 
      ManagementClass classInstance = new ManagementClass(oMs, path, opt); 
      ManagementBaseObject inParams = classInstance.GetMethodParameters("Create"); 
      inParams["CommandLine"] = txtPath.Text; 
      ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null); 
      lblInfo.Text = outParams["returnValue"].ToString() + " Process ID: {0}" + outParams["processId"].ToString(); 

不过,我得到以下错误:

Operation is not valid due to the current state of the object.

有没有人有这个问题更好的解决办法?

顺便说一句,我有以下情形:

我的客户端运行的服务器,他在其中有一些应用程序做许多不同的事情(日志记录,计算......)。他希望能够监控并在需要时从基于Web的客户端(甚至他的手机)重新启动这些应用程序。

我试图用这个应用程序来完成的是杀死一个进程并启动相同的进程。使用System.Diagnostics我能够查询当前正在运行的进程,但这显然不是解决方案,因为对于远程计算机,System.Diagnostics只能看到进程,而不能与进程交互。

+1

你在服务器端做的任何事都会运行服务器端。如果你想在客户端上运行任意代码,那么你需要在客户端有一些应用程序,它能够根据代码片段动态地确定你想要做什么。也许在客户端上运行的服务器上集成[CSScript](http://www.csscript.net/)项目之类的东西会有所帮助。 – 2012-01-05 02:09:11

+0

我正在尝试实现的网站以及正在运行的应用程序实际上都在服务器上运行。现在,如果我不得不重新实现应用程序,我可能会选择将它们实现为Windows服务,但不幸的是,这些应用程序已经很久以前实现了,并且我没有可用的源代码。 一个问题是这些应用程序容易崩溃。要求是能够检测是否已经崩溃,并重新启动它。 – user496607 2012-01-05 02:13:10

+0

那你真的想'使用ASP.Net'在远程机器上执行一个进程吗?如果你是客户端和服务器在同一台机器上,那么它不是真的'远程'吗? – 2012-01-05 02:17:40

回答

0

当您处理WMI并获取列出的错误时,通常意味着没有数据要从您的查询中返回。看着你的代码,似乎你永远不会打电话到存储过程数据的地方。您需要在代码中的某处调用Win32_Process。获取进程信息的代码如下所示:

ManagementPath path = new ManagementPath(@"\\" + txtRemoteComputer.Text + @"\root\cimv2"); 
System.Management.ManagementScope oMs = new System.Management.ManagementScope(path, conO); 
oMs.Connect(); 
ObjectQuery query = new ObjectQuery(
       "SELECT * FROM Win32_Process"); 
ManagementObjectSearcher searcher = 
       new ManagementObjectSearcher(oMs, query); 
foreach (ManagementObject queryObj in searcher.Get()) 
{ 

之后,您可以使用代码进行任何操作。请注意,这段代码只是为了获得这个过程,我没有发送呼叫来远程结束一个进程的经验,但至少在这之后你可以开始检查远程计算机是否在晚上运行某个进程。

相关问题