将剪贴板中的文本发送到应用程序,如记事本(C#或Powershell)
我希望能够将Windows中剪贴板上的文本发送到应用程序。例如,我正在处理记事本中的文本文件,并且我想将一部分复制到一个新文件中。我想将它复制到剪贴板,然后使用热键启动发送应用程序或PowerShell脚本将文本复制到记事本的新实例。将剪贴板中的文本发送到应用程序,如记事本(C#或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的编辑。使用开关/c
它启动记事本2并粘贴剪贴板中的文本。
为了使它更实用,我使用这样定义的函数tnp
: (请注意,您需要使用-Sta参数运行PowerShell的,否则他们将不会对propely工作)
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()
}
随着这些功能可以运行这样的事情:
# gets list of members, opens Notepad2 and pastes the content (members list)
(get-date) | gm | tnp
换句话说 - 如果一些信息将返回并格式化屏幕,你可以得到它,并粘贴到记事本。
为了让您一开始,在优秀PowerShell Community Extensions库有Get-Clipboard
的cmdlet,获取当前剪贴板中的内容的。从那里,它是相当容易做到你想要的剪贴板数据什么的,比如:
Get-Clipboard > test.txt; notepad test.txt
运行上面得到当前剪贴板中的内容,并将其设置为test.txt的,然后在记事本中打开test.txt的。
一(hackish的)策略是:
- 启动过程。
- 激活其主窗口。
- 根据需要模拟击键。
[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)
请在解决问题的原因和方式上添加一些关于您的解决方案的意见 – 2015-12-03 16:54:22
我喜欢这个... I C一旦打开文件,就需要额外的步骤来删除文件,因为我所关心的只是将数据导入编辑器。 – ctorx 2010-09-26 05:10:00