代码之家  ›  专栏  ›  技术社区  ›  ctorx

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

  •  5
  • ctorx  · 技术社区  · 16 年前

    如何在C或Powershell中实现这一点?

    ^+c::
    Send ^c
    Run Notepad
    WinWait Untitled - Notepad
    WinActivate
    Send ^v
    return
    
    5 回复  |  直到 16 年前
        1
  •  3
  •   stej    16 年前

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

    自动热键版本

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

    PowerShell版本

    你需要使用像这样的编辑器 Notepad2 /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() 
    }
    

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

    换句话说——如果一些信息被返回并格式化到屏幕上,你可以得到它并粘贴到记事本上。

        2
  •  2
  •   Eddie Groves    16 年前

    为了让你开始,在最好的 PowerShell Community Extensions Get-Clipboard 获取当前剪贴板内容的cmdlet。从这里开始,您可以对剪贴板数据执行任何操作,例如:

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

    运行以上命令将获取当前剪贴板内容,并将其设置为测试.txt然后打开测试.txt在记事本里。

        3
  •  2
  •   Ani    16 年前

    一个(黑客的)策略是:

    1. 开始这个过程。
    2. 根据需要模拟按键。

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

    在文本编辑器(如notepad)接受文本文件路径作为命令行参数的特定情况下,可以做一些更健壮但不太灵活的操作:

    [STAThread]
    static void Main()
    {
        var tempFilePath = Path.GetTempFileName();
        File.WriteAllText(tempFilePath , Clipboard.GetText());
        Process.Start("Notepad.exe", tempFilePath);
    }
    
        4
  •  1
  •   MaxTag    16 年前

    如果最终使用自动热键,请添加 克利普韦特

    ^+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
    
        5
  •  0
  •   Nader MA    10 年前
        Dim temp = System.IO.Path.GetTempFileName()
        System.IO.File.WriteAllText(temp, Clipboard.GetText())
        Process.Start("Notepad.exe", temp)