.net 如何从 Windows 窗体应用程序打开记事本并在其中放置一些文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5702669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:30:03  来源:igfitidea点击:

How to open Notepad from a Windows Forms application and place some text in it?

.netvb.netwinforms

提问by Sean

I'm using VB.NET and Visual Studio 2008.

我正在使用 VB.NET 和 Visual Studio 2008。

My question is: How do I open Notepad from a Windows Forms application, and then place some text string in the Notepad window?

我的问题是:如何从 Windows 窗体应用程序打开记事本,然后在记事本窗口中放置一些文本字符串?

回答by RB.

The easiest approach is to write a text-file, then open that, rather than the other way round.

最简单的方法是编写一个文本文件,然后打开它,而不是相反。

You can use System.File.IO.WriteAllText, and the System.Diagnostics.Processclass.

您可以使用System.File.IO.WriteAllTextSystem.Diagnostics.Process类。

A quick code-sample would be along these lines:

一个快速的代码示例应该是这样的:

File.WriteAllText (
    @"C:\temp\myFile.txt", 
    "This is my letter header\nIt has a new-line in it")
Process.Start("notepad.exe", @"C:\temp\myFile.txt");

回答by jgauffin

  1. Use Process.Startwith the property ShellExecuteset to true;
  2. Use the clipboard: http://www.dreamincode.net/forums/topic/40011-how-do-i-put-text-in-another-program/
  1. 使用Process.Start属性ShellExecute设置为true;
  2. 使用剪贴板:http: //www.dreamincode.net/forums/topic/40011-how-do-i-put-text-in-another-program/

Update

更新

Process.Startreturns a Processobject which has a MainWindowHandleproperty. Use that handle when sending text instead of the FindWindow in the above mentioned link.

Process.Start返回一个Process具有MainWindowHandle属性的对象。在上述链接中发送文本而不是 FindWindow 时使用该句柄。

Update 2

更新 2

Some code

一些代码

Const WM_SETTEXT As Integer = &HC
<DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, <MarshalAs(UnmanagedType.LPStr)> lParam As String) As IntPtr
End Function

Private Shared Sub Main()
    'ProcessStartInfo is used to instruct the Process class
    ' on how to start a new process. The UseShellExecute tells
    ' the process class that it (amongst other) should search for the application
    ' using the PATH environment variable.
    Dim pis As ProcessStartInfo = New ProcessStartInfo("notepad.exe")
    pis.UseShellExecute = True

    ' The process class is used to start the process
    ' it returns an object which can be used to control the started process
    Dim notepad As Process = Process.Start(pis)

    ' SendMessage is used to send the clipboard message to notepad's
    ' main window.
    Dim textToAdd As String = "Text to add"
    SendMessage(notepad.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, textToAdd)
End Sub

回答by Jodrell

The trick here is to make a text file, and pass it to Notepad as a command line argument, or, if Notepad is the default application for ".txt", you can shell straight to the filename.

这里的技巧是制作一个文本文件,并将其作为命令行参数传递给记事本,或者,如果记事本是“.txt”的默认应用程序,您可以直接进入文件名。

Creating/editing textfile through VB.NET

通过 VB.NET 创建/编辑文本文件

Launch and watch a process from VB.NET 2010

从 VB.NET 2010 启动并观察进程

You can use the arguments collection ProcessStartInfoto pass the filename if required.

ProcessStartInfo如果需要,您可以使用参数集合来传递文件名。