如何将此C#代码转换为批处理文件代码?

时间:2011-05-23 12:12:52

标签: c# batch-file

我已经读过Win32不允许远程调用交互式进程,我怀疑控制台应用程序被Windows认为是交互式的,所以相反,如果我可以将以下代码转换为批处理文件,那么我我希望我可以从客户端远程运行服务器计算机上的批处理文件。如果我错了,请随意纠正这个逻辑。

代码是:

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())的方法调用,并且由于我不熟悉批处理文件,我想知道如何实现相同的目标。

非常感谢

1 个答案:

答案 0 :(得分:0)

如果我理解得很清楚,那么您正在寻找一个命令行来执行一系列命令,这些命令存在于一个名为(C:\ inetpub \ wwwroot \ PRIMEWeb \ Executables \ FlyCommand.txt)的文本文件中

以下是您需要做的事情:

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

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

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