4
String str = Clipboard.GetText();

throws an exception

An unhandled exception of type 'System.StackOverflowException' occurred in PresentationCore.dll

How do I prevent this problem?

Gabe
  • 82,547
  • 12
  • 135
  • 231
Mediator
  • 14,387
  • 34
  • 108
  • 180

3 Answers3

4

You could try:

Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text)

Or take a look here: Clipboard.GetText returns null (empty string)

http://msdn.microsoft.com/es-en/library/system.windows.forms.clipboard.gettext.aspx

Community
  • 1
  • 1
Todd Davies
  • 5,414
  • 8
  • 45
  • 71
0

Look at the accepted answer in this thread:

Link to working code sample

In summary you need to ensure you start or are running in an STAThread with staThread.SetApartmentState(ApartmentState.STA); I also suggest you add a little bit of sleep or wait after join as sometimes the clipboard content is not immediately available when staThread.Join() returns.

Yousha Aleayoub
  • 3,939
  • 4
  • 49
  • 62
Ali Rad
  • 149
  • 1
  • 6
0

Just add one line above the main() method, your code will look like this:

[STAThread]

public static void main()

This solved the problem for me.

Yousha Aleayoub
  • 3,939
  • 4
  • 49
  • 62
Jaideep Dhumal
  • 577
  • 4
  • 5