我正在尝试打开一个记事本实例并向其中写入一个虚拟文本条目。记事本可以安全打开,但上面没有写任何内容。会出现什么问题以及为什么?
using System;
using System.Diagnostics;
using WindowsInput;
using WindowsInput.Native;
class Program
{
static void Main()
{
try
{
Process notepadProcess = new Process();
notepadProcess.StartInfo.FileName = "notepad.exe";
notepadProcess.Start();
InputSimulator simulator = new InputSimulator();
simulator.Keyboard.TextEntry("This is a message that will be written to Notepad");
}
catch (Exception ex)
{
Console.WriteLine("Error opening Notepad: " + ex.Message);
}
}
}
我尝试暂停执行线程,以便有时间让记事本启动,但只有移动鼠标后,我才能输入它。
如果您无法告诉InputSimulator文本应发送到哪个进程,则必须将焦点设置到接收者notepadProcess(发送之前)。 需要注意的是,记事本由一个菜单控件和一个编辑区域控件组成。你的文字的重点必须是后者。 您可以通过单击鼠标来设置。