28

I'd like to make a console application in C#, where the user will type something, let's say "Dave" and then it'll output "Name: Dave" and copy the "Name: Dave" to the users clipboard. So is there a way to have the "Name: " + Console.ReadLine(); copied to the users clipboard automatically?

user2923446
  • 399
  • 1
  • 4
  • 5
  • 2
    possible duplicate of [How to copy the contents of a String to the clipboard in C#?](http://stackoverflow.com/questions/899350/how-to-copy-the-contents-of-a-string-to-the-clipboard-in-c) – germi Oct 31 '13 at 13:33

3 Answers3

52

You'll need to reference a namespace:

using System.Windows.Forms;

Then you can use:

Clipboard.SetText("Whatever you like");

EDIT

Here's a copy and paste solution that works for me

using System;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            Console.WriteLine("Say something and it will be copied to the clipboard");

            var something = Console.ReadLine();

            Clipboard.SetText(something);

            Console.Read();
        }
    }
}
Alex
  • 37,502
  • 51
  • 204
  • 332
  • I'm getting the following error: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows'. Are you missing 'System.Windows.Forms' assembly reference? – user2923446 Oct 31 '13 at 13:41
  • 4
    You will need to add a reference to System.Windows.Forms - right click on project, click Add Reference. – Alex Oct 31 '13 at 13:47
  • Ok thank you, but it isn't copying it to my Clipboard: Console.WriteLine ("Say something and it'll be copied to your clipboard"); string something = Console.ReadLine (); System.Windows.Forms.Clipboard.SetText(something); – user2923446 Oct 31 '13 at 13:54
  • It might work on windows, but it certainly doesn't work on mac:( – user2923446 Oct 31 '13 at 20:32
  • Are you on a wind up? Your question is about C#. NO mention of mac anywhere – Alex Oct 31 '13 at 20:36
  • 1
    I'm coding in MonoDevelop on a Mac. My program is apparently crashing when it tries to add to clipboard, did I mess something up? Here's the code: http://pastebin.com/mJnqu9EX – user2923446 Oct 31 '13 at 20:58
  • 4
    this was CRUCIAL information omitted from the original question. – Alex Oct 31 '13 at 21:28
  • 11
    The [STAThread] attribute MUST be present on the Main method, else this doesn't work. – Marc van Nieuwenhuijzen Aug 20 '16 at 20:33
  • For all of the mentioned problems, look at my answer: https://stackoverflow.com/a/66563643/5827730 – Khalil Youssefi Mar 10 '21 at 11:21
21

Use

System.Windows.Forms.Clipboard.SetText(message)

where message is the string to be copied.

Although the System.Windows.Forms namespace was designed for Windows Forms, many methods from its API have valuable uses even in console / other non-Winforms applications.

Alex Walker
  • 2,337
  • 1
  • 17
  • 32
5

1: You need to add a reference to System.Windows.Forms as follows:

Right-click your project in Solution Explorer and select Add reference... and then find System.Windows.Forms and add it. (look at this answer)

2: Then you can add System.Windows.Forms to your code using the line below and make sure you place it correctly where it should be with other using(s):

using System.Windows.Forms;

3: Add [STAThread] on the top of your Main function, so it should be like this:

[STAThread]
static void Main(string[] args)
{
      ....
}
    

4: Use Clipboard as you like, for example:

Clipboard.SetText("Sample Text");
Khalil Youssefi
  • 385
  • 6
  • 10