C# Command Line Clipboard

I have a client that wanted a program that would put whatever is passed to it into the clipboard:

C:\> ClipIt.exe This all goes into the clipboard

Its relatively simple.  I did this as a console app, so if you do this as a Winform, skip the first step.

1. Add a reference to System.Windows.Forms to give you access to the ClipBoard class.

2. Add this code to your Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ClipIt
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string clipped = "";
            foreach (string s in args)
            {
                clipped += s + " ";
            }
            Clipboard.SetText(clipped.Trim());
        }
   }
}

3. That’s it. Now you can pass it text, and it will put it into the Windows Clipboard.

Leave a Reply

Your email address will not be published. Required fields are marked *