我为我提供了一个cmd行exe,我无法对其进行更改,我需要编写一个脚本,但是它内置了Pause功能,并且看不到任何方法可以跳过此暂停,因此其余脚本可以继续。
我尝试了各种方法,包括
感谢任何建议
最佳答案
您可以使用互操作将数据发送到进程。这称为挂接进程,上面有一些资源。我喜欢这个answer。
This is a little code that allows you to send message to a backgrounded application. To send the "A" char for example, simply call
sendKeystroke(Keys.A)
, and don't forget to use namespace System.windows.forms to be able to use the Keys object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace keybound
{
class WindowHook
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public static void sendKeystroke(ushort k)
{
const uint WM_KEYDOWN = 0x100;
const uint WM_SYSCOMMAND = 0x018;
const uint SC_CLOSE = 0x053;
IntPtr WindowToFind = FindWindow(null, "Untitled1 - Notepad++");
IntPtr result3 = SendMessage(WindowToFind, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);
//IntPtr result3 = SendMessage(WindowToFind, WM_KEYUP, ((IntPtr)c), (IntPtr)0);
}
}
}
您可能会比他们获得应用程序而不是搜索过程要容易得多,因为您可以从应用程序中启动它:
Process proc = new Process();
proc.StartInfo.FileName = executablePath;
proc.Start();
proc.WaitForInputIdle();
然后
proc.Id
将成为PID。作为替代方案,我遇到了VB类型的example,使用Shell函数似乎更简单,但是我以前从未使用过。您需要在应用程序中添加一个暂停以等待提示,但是,与Interop相比,它看起来更干净:
Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
' Send the keystrokes to the Calculator application.
My.Computer.Keyboard.SendKeys("22", True)
My.Computer.Keyboard.SendKeys("*", True)
My.Computer.Keyboard.SendKeys("44", True)
My.Computer.Keyboard.SendKeys("=", True)
' The result is 22 * 44 = 968.
如果最后使用
System.ArgumentException
,则可能是因为Shell函数没有获取进程ID。这是因为它需要完全信任。如果以管理员身份运行,该应用程序将正常运行。如果您无法做到这一点,我认为您不会找到一种简便的方法,因为使应用程序彼此运行是一个安全问题,但是我可能是错的。
关于windows - 在外部exe程序上跳过 'press any key to continue',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31139989/