(50分)通过Process打开notepad.exe后,如何向记事本中写入信息呢?

dazhu2 2009-12-18 11:14:19
通过Process打开notepad.exe后,如何向记事本中写入信息呢?
代码如下:
Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardError=true;
p.StartInfo.UseShellExecute = false;
p.Start();

p.StandardInput.WriteLine(" 计算结果如下:");

但打开的记事本中为空,没有想写入的字符,请问该如何实现改功能?
...全文
258 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dazhu2 2009-12-19
  • 打赏
  • 举报
回复
ok
wuyq11 2009-12-18
  • 打赏
  • 举报
回复
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public const uint WM_SETTEXT = 0x000C;

System.Diagnostics.Process Proc;
try
{
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "notepad.exe";
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.Start();
}
catch
{
Proc = null;
}
if (Proc != null)
{
while (Proc.MainWindowHandle == IntPtr.Zero)
{
Proc.Refresh();
}
IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
SendMessage(vHandle, WM_SETTEXT, 0, "Message");
}
}

111,095

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧