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

发布于 2024-11-09 08:05:06 字数 1750 浏览 0 评论 0原文

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

非常感谢

I have read that Win32 will not allow remote invocation of a process that is interactive and I suspect a Console Application is considered to be interactive by Windows and so instead, if I could convert the following code in to a batch file then I am hoping I can remotely run the batch file on the server computer from a client. Feel free to correct this logic if I'm wrong.

The code is:

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;
    }

}
}

As you will notice the code contains method calls of Windows library methods like SetForegroundWindow() and as I am unfamiliar with batch files, I wondered how the same thing might be achieved.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-11-16 08:05:06

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

您需要执行以下操作:

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

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

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

If I understand well, you are looking for a command line that will execute a bunch of commands that exist inside a text file named (C:\inetpub\wwwroot\PRIMEWeb\Executables\FlyCommand.txt)

Here is what you need to do:

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

In case the path contains spaces, use the following command:

cmd < "C:\inetpub\wwwroot\PRIMEWeb\Executables\FlyCommand.txt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文