How do I save a copy of the clipboard and then revert back to it?

Posted by cksubs on Stack Overflow See other posts from Stack Overflow or by cksubs
Published on 2010-02-11T08:52:19Z Indexed on 2010/03/13 4:07 UTC
Read the original article Hit count: 299

Filed under:
|

I'm reading words in a text box by simulating the key presses required to highligh and copy the text. When I'm done, I want the clipboard to be exactly how I found it.

I was hoping I'd be able to do something like this:

IDataObject clipboardBackup = Clipboard.GetDataObject();
Clipboard.Clear();
//Save other things into the clipboard here, etc//
Clipboard.SetDataObject(clipboardBackup);

But that doesn't seem to work. It looks like you can go the route of specifically trying for text, audio, pictures, etc. and then saving them accordingly. (I guess 'data object' is specialized like that in my example too, I was hoping it was generic.)

I'd prefer not to use cases for every possible type of clipboard data, both to be more concise and to make sure I never lose the data regardless of format.

Any tips for grabbing any and all of the clipboard and then restoring it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about clipboard