František Žiačik
František Žiačik

Reputation: 7614

How to determine encoding of Clipboard contents

I have a 3rd party application from which I need to copy texts and paste it into visual studio. However, when I copy the text like vysvedčenie and paste into Visual Studio text editor, I get vysvedèenie.

I bet it's due to the other program putting non-unicode encoded text into clipboard. So I made a program to periodically check clipboard and convert the text into unicode like this:

var originalText = Clipboard.GetText(TextDataFormat.Text);
Clipboard.SetText(originalText, TextDataFormat.UnicodeText);

This works fine, but the problem is how do I determine I already have a unicode-encoded text in the clipboard so that I don't try to convert it again?

I thought that Clipboard.ContainsText(TextDataFormat.UnicodeText) would work, but this always returned true.

Upvotes: 3

Views: 3853

Answers (2)

Orin
Orin

Reputation: 371

Also note: Note from MSDN documentation: The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

Upvotes: 0

Aliostad
Aliostad

Reputation: 81680

Use Clipboard.GetDataObject().

Then you can call GetFormats() on the IDataObject returned.

Upvotes: 3

Related Questions