将剪贴板中的文本发送到应用程序,如记事本(C#或Powershell)

我希望能够将剪贴板(在Windows中)上的文本发送到应用程序。 例如,我正在写一个记事本中的文本文件,我想将一部分复制到一个新文件中..我想将它复制到剪贴板然后使用热键启动发送的应用程序或powershell脚本将文本复制到记事本的新实例。

如何在C#或Powershell中实现这一目标?

解决方案:使用AutoHotKey

^+c:: Send ^c Run Notepad WinWait Untitled - Notepad WinActivate Send ^v return 

我有2个解决方案,一个使用PowerShell,另一个使用Autohotkey 。

Autohotkey版本

我会使用这个;)您定义绑定到键的自定义键和操作。 我的文件包含以下代码:

 ^#n:: Run, Notepad WinWaitActive Untitled - Notepad2 Send !e Send p return 

它运行notepad2,然后模拟按Alt + E和P.粘贴字符串的方式与您自己按下它的方式相同。 出于某种原因,我在按“Ctrl + V”时出现了一些问题(我不记得了)。 有关更多信息,请查看Autohotkey的网站。

PowerShell版本

您需要使用像Notepad2这样的编辑器。 使用switch /c它会启动Notepad2并粘贴剪贴板中的文本。

为了使它更有用,我使用如下定义的函数tnp 🙁 请注意,您需要使用-sta参数运行PowerShell,否则它们将无法正常工作)

 function tnp { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [object] $InputObject ) begin { $objs = @() } process { $objs += $InputObject } end { $old = Get-clipboard # store current value $objs | out-string -width 1000 | Set-Clipboard notepad /c sleep -mil 500 $old | Set-Clipboard # restore the original value } } function Set-Clipboard { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)][object]$s ) begin { $sb = new-object Text.StringBuilder } process { $s | % { if ($sb.Length -gt 0) { $null = $sb.AppendLine(); } $null = $sb.Append($_) } } end { Add-Type –a system.windows.forms; [windows.forms.clipboard]::SetText($sb.Tostring()) } } function Get-Clipboard { Add-Type –a system.windows.forms [windows.forms.clipboard]::GetText() } 

使用这些function,您可以运行以下内容:

 # gets list of members, opens Notepad2 and pastes the content (members list) (get-date) | gm | tnp 

换句话说 – 如果某些信息将被返回并格式化为屏幕,您可以获取它并粘贴到记事本。

为了帮助您入门,在优秀的PowerShell社区扩展库中,有Get-Clipboard cmdlet可以获取当前剪贴板的内容。 从那里用剪贴板数据做任何你想要的事情是相当微不足道的,例如:

 Get-Clipboard > test.txt; notepad test.txt 

运行上面的内容获取当前剪贴板内容,将它们设置为test.txt,然后在记事本中打开test.txt。

一个(hackish)策略是:

  1. 开始这个过程。
  2. 激活其主窗口。
  3. 根据需要模拟击键。

 [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [STAThread] static void Main() { var p = Process.Start("Notepad.exe"); p.WaitForInputIdle(); SetForegroundWindow(p.MainWindowHandle); // this can probably be left out. SendKeys.SendWait(Clipboard.GetText()); } 

在像记事本这样的文本编辑器接受文本文件路径作为命令行参数的特定情况下,您可以执行更强大但更不灵活的操作:

 [STAThread] static void Main() { var tempFilePath = Path.GetTempFileName(); File.WriteAllText(tempFilePath , Clipboard.GetText()); Process.Start("Notepad.exe", tempFilePath); } 

如果你最终使用AutoHotKey,添加ClipWait以确保AutoHotKey等待Windows实际更改剪贴板

 ^+c:: Send ^c ClipWait Run Notepad WinWait Untitled - Notepad WinActivate Send ^v return 

如果您只想使用剪贴板作为传输文本的临时方法(因此不会丢失之前保存在剪贴板中的内容),您可以添加如下内容:

 ^+c:: ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice. Send ^c ClipWait ; Wait for the clipboard to change Run Notepad WinWait Untitled - Notepad WinActivate Send ^v Clipboard := ClipSaved ; Restore the original clipboard. ClipSaved = ; Free the memory in case the clipboard was very large. return 
  Dim temp = System.IO.Path.GetTempFileName() System.IO.File.WriteAllText(temp, Clipboard.GetText()) Process.Start("Notepad.exe", temp)