2

If the current (most recent) item in the clipboard is a string, I want to take the string from the clipboard, and put each word in an array (just a simple loop which eliminates spaces and newlines, to take just words from the string). Then I simply want to print each item in the array (let's say each separated by a newline for testing purposes) to the terminal.

This is super easy, simple, etc. My trouble is that I cannot seem to find/use the Clipboard class that is built in to C#! Here is what I am using as my assembly references:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Object;
using System.Windows.Clipboard;

Note that the System.Windows.Clipboard; gets a red underline, because it is not lining up. Why could this be? http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx shows the clipboard class information.

It's probably a fault on my end, but is it otherwise possible I have not set my paths correctly or something? I just installed Visual Studio 2012 For Desktop SP3 (although, I have had VS2012 for Web for quite some time, and it works properly).

H H
  • 263,252
  • 30
  • 330
  • 514
Musicode
  • 661
  • 1
  • 12
  • 29
  • What GUI? Your namespace is for WPF. – H H Sep 07 '13 at 19:16
  • Console (cmd prompt). – Musicode Sep 07 '13 at 19:18
  • If you read the link in your post it says "Assembly: PresentationCore.dll" - make sure you have reference to that assembly (also I believe WPF project must have it)... Side note: there is no "classes built in to C#" - theses are .Net Framework classes. – Alexei Levenkov Sep 07 '13 at 19:19
  • @KinkKing: The OP wants to "print to the terminal". – H H Sep 07 '13 at 19:19
  • It was added automatically honestly idk how that happened, sorry. – Musicode Sep 07 '13 at 19:21
  • What is the actual compiler error message? – Timbo Sep 07 '13 at 19:23
  • "Clipboard: Name not found" was the error. I fixed it by adding presentation core to the project references. – Musicode Sep 07 '13 at 19:24
  • possible duplicate of [Copy result to clipboard](http://stackoverflow.com/questions/14082942/copy-result-to-clipboard), there are also many examples that can be found by searching for [C# clipboard console](http://www.bing.com/search?q=c%23+clipboard+console) (link to bing, feel free to use any other search engine) – Alexei Levenkov Sep 07 '13 at 19:24
  • @DNA_Instant After you removed `using System.Windows.Clipboard;` obviously, because keeping that would have caused another error. – Timbo Sep 07 '13 at 19:25
  • @AlexeiLevenkov - check the edit history, somebody else added [WPF] – H H Sep 07 '13 at 19:27

6 Answers6

2

System.Windows.Clipboard is a class and not a namespace, there is no point putting a class into a using directive.

The compiler error message should tell you the same.

Timbo
  • 27,472
  • 11
  • 50
  • 75
2

First of all you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened. You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.

using System;

using System.Windows.Forms;

class Program {

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

     Clipboard.SetText("this is in clipboard now");
}

}

Amr Reda
  • 632
  • 7
  • 18
1

i hope this will help you

http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/

it shows how to use the Clipboard functions in C# console application

Nithin Nayagam
  • 458
  • 2
  • 5
  • 9
  • You should always try to take a summary from a link. In this case it's for System.Windows.Forms, the other option. – H H Sep 07 '13 at 19:43
0

You don't need this line: using System.Windows.Clipboard; using statements are for namespaces, and Clipboard is a class. That class is provided from the assembly PresentationCore.dll, and since you have WPF project, your project already has a reference to it.

Chris O
  • 5,017
  • 3
  • 35
  • 42
0

You should have reference to System.Windows namespace only it has Class Clipboard in it. You can set and get data from there

System.Windows.Clipboard.SetData();
Nitin
  • 18,344
  • 2
  • 36
  • 53
0

You'll need a namespace declaration:

using System.Windows.Forms;

OR for WPF:

using System.Windows;

To copy an exact string

var dataObject = System.Windows.Clipboard.GetDataObject();
string text= dataObject.GetData("UnicodeText", true).ToString();
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36