Mediator
Mediator

Reputation: 15378

Why doesn't Clipboard.GetText work?

String str = Clipboard.GetText();

throws an exception

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

How do I prevent this problem?

Upvotes: 4

Views: 14070

Answers (3)

Ali Rad
Ali Rad

Reputation: 159

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.

Upvotes: 0

Jaideep Dhumal
Jaideep Dhumal

Reputation: 1047

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.

Upvotes: 1

Todd Davies
Todd Davies

Reputation: 5522

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

Upvotes: 4

Related Questions