通过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(" 计算结果如下:");但打开的记事本中为空,没有想写入的字符,请问该如何实现改功能?

解决方案 »

  1.   

    [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");
        }