2011-05-23 54 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"