Strange clipboard behavior in c # console application

Consider this small program:

class Program { [STAThread] static void Main(string[] args) { Console.WriteLine("Please copy something into the clipboard."); WaitForClipboardChange(); Console.WriteLine("You copied " + Clipboard.GetText()); Console.ReadKey(); } static void WaitForClipboardChange() { Clipboard.SetText("xxPlaceholderxx"); while (Clipboard.GetText() == "xxPlaceholderxx" && Clipboard.GetText().Trim() != "") Thread.Sleep(90); } } 

I ran it and I will copy the line from Notepad. But the program just gets an empty line from the clipboard and writes "You copied".

What is the problem? Is there anything that makes clipboard accession believable in a console application?

This is a Windows 7 SP1 x86 client profile, .NET 4.

+8
c # clipboard console winforms
source share
4 answers

Use this function

 static string GetMeText() { string res = "starting value"; Thread staThread = new Thread(x => { try { res = Clipboard.GetText(); } catch (Exception ex) { res = ex.Message; } }); staThread.SetApartmentState(ApartmentState.STA); staThread.Start(); staThread.Join(); return res; } 

In this line:

  Console.WriteLine("You copied " + Clipboard.GetMeText()); 

The problem is that the clipboard only works with certain stream models (ApartmentState.STA), so you need to create a new stream and give it a model that this code does.

+9
source share

I can reproduce the problem with your code in the .NET Profile Client, but when switching to .NET 4 or 4.5 it works as expected.

However, the ClipBoard.GetText() manual says:

Use the ContainsText method to determine if the clipboard contains text data before retrieving it using this method.

I accept this as an instruction, not a suggestion, so try the following:

 class Program { [STAThread] static void Main(string[] args) { Console.WriteLine("Please copy something into the clipboard."); WaitForClipboardChange(); Console.WriteLine("You copied " + Clipboard.GetText()); Console.ReadKey(); } static void WaitForClipboardChange() { Clipboard.Clear(); while (!Clipboard.ContainsText()) Thread.Sleep(90); } } 

It shows the copied text, although I must say that it allows my system to get corrupted when I copy the text.

+5
source share

This works for me:

 static void Main(string[] args) { Console.WriteLine("Please copy something into the clipboard."); string text = WaitForClipboardChange(); Console.WriteLine("You copied " + text); } static string WaitForClipboardChange() { string placeholderText = "xxPlaceholderxx"; Clipboard.SetText(placeholderText); string text = null; do { Thread.Sleep(90); text = Clipboard.GetText(); } while (string.IsNullOrWhiteSpace(text) || text.Equals(placeholderText)); return text; } 
+3
source share

Your current code is clearly waiting for the first change from "xxPlaceholderxx" to something (your condition is "an undefined string AND not empty", which turns false as soon as the string changes from "xxPlaceholderxx" to anything, including "" ):

 while (Clipboard.GetText() == "xxPlaceholderxx" && Clipboard.GetText().Trim() != "") 

You probably want || (or) instead of:

 // assuming System.Windows.Forms.Clipboard static void WaitForClipboardChange() { Clipboard.SetText("xxPlaceholderxx"); while (Clipboard.GetText() == "xxPlaceholderxx" || string.IsNullOrWhiteSpace(Clipboard.GetText())) Thread.Sleep(90); } 
+1
source share

All Articles