Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Last active December 31, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/7956271 to your computer and use it in GitHub Desktop.
Save jpluimers/7956271 to your computer and use it in GitHub Desktop.
PasteText: the reverse of clip.exe; pastes Clipboard.GetText() or Clipboard.GetFileDropList() to the standard output.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Forms;
namespace PasteText
{
class Program
{
private const string STR_PasteText = "PasteText.exe";
[STAThread]
static void Main(string[] args)
{
bool help = showHelp(args);
if (!help)
pasteText(args);
}
private static bool showHelp(string[] args)
{
bool help = args.Contains("/?") || args.Contains("-?") || args.Contains("/h") || args.Contains("-h") || args.Contains("--help");
if (help)
{
Console.Out.WriteLine(@"{0} [/?|-?|/h|-h|--help] [/r|-r|--raw]
pastes Clipboard.GetText() or Clipboard.GetFileDropList() to the standard output
Optional arguments:
/?|-?|/h|-h|--help:
shows this help
/r|-r|--raw:
pastes raw text (default: pastes text as is)
Example:
{0} | clip.exe
pastes the text through clip.exe, effectively stripping any formatting.", STR_PasteText);
}
return help;
}
private static void pasteText(string[] args)
{
if (Clipboard.ContainsText())
{
bool rawOutput = args.Contains("/r") || args.Contains("-r") || args.Contains("--raw");
string text = Clipboard.GetText();
if (rawOutput)
{
Console.Out.Write(text);
}
else
{
// the order of the separators is important: CRLF, LFCR, LF, CR
// http://msdn.microsoft.com/en-us/library/vstudio/tabh47cf
// TODO: in the future add more Unicode separators
// http://en.wikipedia.org/wiki/Newline#Representations
// http://en.wikipedia.org/wiki/Newline#Unicode
string[] separators = new[] { "\r\n", "\n\r", "\n", "\r", };
string[] lines = text.Split(separators, StringSplitOptions.None);
foreach (var line in lines)
{
Console.Out.WriteLine(line);
}
}
}
else if (Clipboard.ContainsFileDropList())
{
StringCollection files = Clipboard.GetFileDropList();
foreach (string file in files)
{
Console.Out.WriteLine(file);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment