目前我遇到了一个问题,就是如何运行于远程机器上的一个Exe文件。
假设这个文件放在目标机器c盘的根目录,文件名为test.exe,如何才能在Remoting基础下从远程运行这个文件呢?
欢迎大家讨论,提供一个思路,谢谢!

解决方案 »

  1.   

    可以用Process.Start启动一个进程
    http://chs.gotdotnet.com/quickstart/howto/doc/ProcessDemo.aspx然后有remoting基础的话应该就知道怎么做了http://chs.gotdotnet.com/quickstart/howto/doc/Remoting/mainfeatures.aspx
      

  2.   

    remote.vb
    Imports SystemPublic Class ServiceClass
       Inherits MarshalByRefObject   Private m_starttime As DateTime   Public Sub New()
          Console.WriteLine("ServiceClass created without constructor. Instance hash is " & Me.GetHashCode().ToString())
          m_starttime = DateTime.Now
       End Sub      Overrides Protected Sub Finalize()
          Console.WriteLine("I'm being collected after " & (New TimeSpan(DateTime.Now.Ticks - m_starttime.Ticks)).ToString() & " seconds.")
          MyBase.Finalize()
       End Sub       Public Function GetServerTime() As DateTime
          Console.WriteLine("Time requested by a client.")
          Return DateTime.Now
       End Function  
       
       Public Sub StartProcess()  
           System.Diagnostics.Process.Start("C:\abc.exe")
       End Sub   Public ReadOnly Property InstanceHash() As Integer
          Get
             Return Me.GetHashCode()
          End Get
       End Property End Class
    Server.vb
    Imports System
    Imports System.Runtime.Remoting
    Imports System.Runtime.Remoting.Channels
    Imports System.Runtime.Remoting.Channels.HttpPublic Class ServerProcess
       <MTAThread()> _
       Public Shared Sub Main()      Dim channel As New HttpChannel(8080)
          ChannelServices.RegisterChannel(channel)      Dim object1 As New ServiceClass()      ' Creates the single instance of ServiceClass. All clients
          ' will use this instance.
          Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
          Console.WriteLine("ObjRef.URI: " & ref1.URI)      Console.WriteLine("Running. Press Enter to end publication.")
          Console.ReadLine()      ' This unregisters the object from publication, but leaves
          ' the channel listening.
          RemotingServices.Disconnect(object1)
          Console.WriteLine()
          Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
          Console.WriteLine("Press Enter to unregister the channel.")
          Console.ReadLine()
          ' At this point, the ServerClass object still exists. The server
          ' could republish it.      ' This unregisters the channel, but leaves the application 
          ' domain running.
          ChannelServices.UnregisterChannel(channel)
          Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
          ' The ServerClass object still exists. The server could
          ' reregister the channel and republish the object.
          Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
          Console.ReadLine()      ' The ServiceClass object's Finalize method writes a message to
          ' the console. A single object will almost always succeed in 
          ' running its Finalize method before the Console is finalized;
          ' in a larger application, you could ensure that all objects 
          ' finalize before the application ends by calling the garbage 
          ' collector and waiting.
          GC.Collect()
          GC.WaitForPendingFinalizers()
       End Sub
       
    End Class
    Client.vb
    Imports System 
    Imports System.Runtime.Remoting
    Imports System.Runtime.Remoting.Channels
    Imports System.Runtime.Remoting.Channels.Tcp
    Imports System.Runtime.Remoting.Channels.HttpPublic Class ClientProcess
       <MTAThread()> _
       Public Shared Sub Main()
          
          Dim channel As New HttpChannel(0)
          ChannelServices.RegisterChannel(channel)      ' Registers the remote class. (This could be done with a
          ' configuration file instead of a direct call.)
          RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("ServiceClass, remote"), "http://localhost:8080/object1uri")      ' Instead of creating a new object, this obtains a reference
          ' to the server's single instance of the ServiceClass object.
          Dim object1 As ServiceClass = New ServiceClass()      Try
             Console.WriteLine("ServerTime: " & object1.GetServerTime())
             object1.StartProcess()
          Catch ex As Exception
             Console.WriteLine("Exception of type: " & ex.GetType.ToString & " occurred.")
             Console.WriteLine("Details: " & ex.Message)
          End Try   End Sub     ' Main
    End Class   ' ClientProcess
      

  3.   

    Process类应该可以控制远程机器上的进程,可是我目前的问题在于如何启动一个在远程机器上的可运行文件,而不是在运行后如何控制它(根本就还没有使这个文件运行起来呢)。
      

  4.   

    可以用telnet远程登陆
    然后再运行!!
      

  5.   

    仅供参考,注意更换里面的用户名口令和计算机另外用户需要有登录和执行程序的权限ConnectionOptions options = new ConnectionOptions();
    options.Username = "user";
    options.Password = "password";
    options.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope scope = new ManagementScope(
    @"\\remoteMachine\root\cimv2", options);
    scope.Connect();
    ManagementPath path = new ManagementPath("Win32_Process");
    ManagementClass process = new ManagementClass(
             scope, path, new ObjectGetOptions());
    process.InvokeMethod("Create", 
    new object[] {"notepad.exe", null, null, 0}));
      

  6.   

    对于服务器激活的可远程处理的类型,.NET 远程处理仅支持默认构造函数。如果要在使用某个特定构造函数创建对象后发布该对象并且完全控制该特定实例的发布,可以通过编程方式发布该实例。警告   .NET 远程处理在默认情况下不进行身份验证或加密。因此,建议您在与客户端或服务器进行远程交互之前,采取任何必要的措施确认它们的身份。因为 .NET 远程处理应用程序需要 FullTrust 权限才能执行,所以未经授权的客户端如果被授予了访问您的服务器的权限,该客户端就可能像完全受信任的客户端一样执行代码。应始终验证终结点的身份并将通信流加密,通过在 Internet 信息服务 (IIS) 中承载远程类型,或者通过生成自定义通道接收对来完成这项工作。
    编译和运行该示例 在命令提示处键入以下命令: 
    vbc -t:library remote.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll server.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll client.vb 打开两个指向同一目录的命令提示。在其中一个键入 server。在另一个键入 client。 
    若要分步取消发布该可远程处理对象,请在服务器命令提示下按 ENTER 键并重新运行客户端以观察为不同步骤引发的不同异常。该应用程序可在单台计算机上运行或通过网络运行。如果要在网络上运行该应用程序,必须用远程计算机的名称替换客户端配置中的“localhost”。
      

  7.   

    不难,我认为在客户端写一个调用事件,然后再在事件中检测、调用关闭exe的进程就可以了。
      

  8.   

    sorry,没有看好题目,还以为是在客户端运行呢?哈哈
      

  9.   

    研究了半天WMI还是一头雾水,谁有比较系统的资料啊。
      

  10.   

    为了200分,豁出去了。一步一步教你1、新建空白解决方案 RemoteStart
    2、在解决方案中添加类库项目 RemoteStartClass (作为远程对象),将默认Class1.cs删除,新建个类 RemoteStartClass.cs
    using System;namespace RemoteStart
    {
    /// <summary>
    /// RemoteStartClass 的摘要说明。
    /// </summary>
    public class RemoteStartClass: System.MarshalByRefObject 
    {
    public RemoteStartClass()
    {

    } public void RunExeFile()
    {
    System.Diagnostics.Process.Start("notepad.exe");
    }
    }
    }3、在解决方案中添加应用程序项目 RemoteStartServer (作为服务器端程序)
       添加对项目RemoteStartClass及System.Runtime.Remoting的引用
       Form1.cs 如下
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;
    namespace RemoteStart
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(208, 181);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Closed += new System.EventHandler(this.Form1_Closed); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    RemoteStartClass rsc = null;
    HttpChannel channel = null;
    private void Form1_Load(object sender, System.EventArgs e)
    {
    channel = new HttpChannel(10000);
    ChannelServices.RegisterChannel(channel); rsc = new RemoteStartClass(); 
    ObjRef rscref = RemotingServices.Marshal(rsc, "RemoteStartUri") ; } private void Form1_Closed(object sender, System.EventArgs e)
    {
    RemotingServices.Disconnect(rsc);
                ChannelServices.UnregisterChannel(channel);
    }
    }
    }
      

  11.   

    4、在解决方案中添加应用程序项目 RemoteStartClient (作为客户端程序)
       添加对项目RemoteStartClass及System.Runtime.Remoting的引用
       Form1.cs 如下
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Http;namespace RemoteStart
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(48, 64);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(96, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "启动远程程序";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(184, 165);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Closed += new System.EventHandler(this.Form1_Closed);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } RemoteStartClass rsc = null;
    HttpChannel channel = null; private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    rsc.RunExeFile();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }

    } private void Form1_Load(object sender, System.EventArgs e)
    {
    channel = new HttpChannel(0);
    ChannelServices.RegisterChannel(channel);            //URI 替换为相应服务器名(或IP)及端口即可,端口对应RemoteStartServer中注册的端口
    RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("RemoteStart.RemoteStartClass, RemoteStartClass"), "http://LFY:10000/RemoteStartUri"); rsc = new RemoteStartClass(); 

    } private void Form1_Closed(object sender, System.EventArgs e)
    {
    ChannelServices.UnregisterChannel(channel);
    }
    }
    }编译后调试
    将RemoteStartClass.dll,RemoteStartServer.exe部署在服务器端,RemoteStartClass.dll,RemoteStartClient.exe部署在客户端,
    服务器端运行RemoteStartServer启动服务后,客户端运行RemoteStartClient后点击“运行远程程序”按纽,服务器将会打开一个记事本
      

  12.   

    先告诉你怎么做,至于运行原理就得你自己好好研究.net的远程对象了
      

  13.   

    To: pupo,
        非常感谢您这么热心的帮忙,毕竟像您这样的人已经不多了。我想我已经理解你的意思了,就是说在目标机器上预先放置一个demon进程,这样就可以让它来运行所给目录下的文件了。但是,我这里可能还需要从本地机器向目标机器上COPY文件,这个恐怕就不能通过这样的方法来实现了,大概WMI是个比较通用的方法。
      

  14.   

    public class RemoteStartClass: System.MarshalByRefObject 
    {
    public RemoteStartClass()
    {

    }
                       
                      public void CopyFile(byte[] fileByte)
                      {
                           //这里根据客户端带入的参数生成文件
                      } public void RunExeFile()
    {
    System.Diagnostics.Process.Start("notepad.exe");
    }
    }