2011-05-23 55 views
0

我已阅读的Win32不会允许这个过程是互动的远程调用,我怀疑一个控制台应用程序被认为是由Windows互动等,而不是,如果我能在下面的代码转换成一个批处理文件,然后我希望我可以从客户端远程运行服务器计算机上的批处理文件。如果我错了,请随时纠正这个逻辑。我会如何转换此C#代码在批处理文件中的代码?

的代码是:

namespace PRIMEWebFlyControl 
{ 
    class Program 
    { 

    // name of the process we will retrieve a handle to 
    private const string PROCESS_NAME = "PRIMEPipeLine"; 
    private static Process ProgramHandle; 
    private static string command; 

    static void Main(string[] args) 
    { 
     //Console.WriteLine("This program has been launched remotely!"); 
     TextReader tr = new StreamReader("C:\\inetpub\\wwwroot\\PRIMEWeb\\Executables\\FlyCommand.txt"); 
     command = tr.ReadLine(); 
     tr.Close(); 

     ExecuteCommand(); 
    } 

    [DllImport("user32.dll")] 
    static extern bool SetForegroundWindow(IntPtr handle); 

    private static void ExecuteCommand() { 
     if (AssignProcessHandle()) { 
      IntPtr p = ProgramHandle.MainWindowHandle; 
      SetForegroundWindow(p); 
      SendKeys.SendWait(command + "~"); // "~" is equivalent to pressing Enter 
     } 
    } 



    private static bool AssignProcessHandle() 
    { 
     // ask the system for all processes that match the name we are looking for 
     Process[] matchingProcesses = Process.GetProcessesByName(PROCESS_NAME); 
     // if none are returned then we haven't found the program so return false; 
     if (matchingProcesses.Length == 0) return false; 
     // else, set our reference to the running program 
     ProgramHandle = matchingProcesses[0]; 

     // return true to indicate we have assigned the ref sucessfully 
     return true; 
    } 

} 
} 

正如你会发现代码中包含的Windows库的方法,如SetForegroundWindow(方法调用)和我不熟悉批处理文件,我想知道同样的事情会如何实现。

非常感谢

+0

您想发送命令到一些特定的应用程序?可能的解决方案之一是使用您提供的代码运行小型服务器应用程序,该代码接受远程命令并在本地重复它们。这可以通过使用Remoting或WCF(在win应用程序中托管)来完成。 – VikciaR 2011-05-23 12:26:37

+0

要尝试的另一件事是psexec。此外,PowerShell有中远程执行的东西的一种手段,但它比psexc的 – BlackICE 2011-05-23 12:32:02

+0

我有什么是链接到一个WCF服务的ASP页面非常扭曲。我想传递发送密钥的程序总是在服务器计算机上运行,​​所以我构建了第二个轻量级控制台应用程序,由WCF服务启动,该服务仅包含上面显示的代码,该代码查找正在运行的进程并发送密钥在退出之前。运行测试ASP页报告成功执行,但在我的服务器计算机上,程序不可见(即使让控制台等待输入而不关闭)。所以我试图找到一个解决方案,但现在批量似乎不是答案 – ComethTheNerd 2011-05-23 12:37:52

回答

0

如果我没有理解好,你正在寻找一个命令行,将执行了一堆名为(C一个文本文件中存在的命令:\的Inetpub \ wwwroot的\ PRIMEWeb \可执行文件\ FlyCommand。 TXT)

以下是你需要做的:

cmd < C:\inetpub\wwwroot\PRIMEWeb\Executables\FlyCommand.txt 

如果路径中包含空格,请使用以下命令:

cmd < "C:\inetpub\wwwroot\PRIMEWeb\Executables\FlyCommand.txt"