Take the 2-minute tour ×

I have a program that uses clipboard but I want to restore the clipboard to its former state after I am done with it.

This is my code :

IDataObject temp = Clipboard.GetDataObject();

//Some stuff that change Cliboard here
//Some stuff that change Cliboard here




share|improve this question
What is this, WinForms? WPF? –  Jeff Mercado Jun 7 '11 at 8:26
Windows form application –  user779444 Jun 7 '11 at 9:03
add comment

3 Answers

up vote 1 down vote accepted

I cannot confirm whether this will work, but I see no reason why you shouldn't be able to back up the data using the longer approach of actually reading the data and restoring it afterwards.

Read here: http://msdn.microsoft.com/en-us/library/system.windows.forms.idataobject.aspx

You would do something like (pseudo-code)

var lBackup = new Dictionary<string, object>();
var lDataObject = Clipboard.GetDataObject();
var lFormats = lDataObject.GetFormats(false);
foreach(var lFormat in lFormats)
  lBackup.Add(lFormat, lDataObject.GetData(lFormat, false);

//Set test data

//Would be interesting to check the contents of lDataObject here

//Restore data
foreach(var lFormat in lFormats)
//This might be unnecessary
share|improve this answer
add comment

This cannot be done. You cannot backup/restore the clipboard without causing unintended consequences. Please see my post on a similar question. My answer is the one that starts with "It's folly to try to do this".

How do I backup and restore the system clipboard in C#?

Furthermore, I suspect that your motivation for wanting to backup/restore the clipboard is because you want to use it as a crutch to move data, without the user's knowledge or consent. Please read: http://www.clipboardextender.com/developing-clipboard-aware-programs-for-windows/common-general-clipboard-mistakes and http://www.flounder.com/badprogram.htm#clipboard

Lastly, please read and understand this quote:

“Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.” — Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992

share|improve this answer
It is all true but you probably never worked with PowerPoint API. Using of clipboard is the only way to copy shapes/slides/notes from one presentation to another. See stackoverflow.com/questions/5073988/… stackoverflow.com/questions/5740749/… stackoverflow.com/questions/15043767/… –  utapyngo Mar 14 '13 at 6:35
Or even old school Word CommandBarButton custom icons... you had to copy the image to the clipboard and then use PasteFace() –  Net Dev Nov 7 '13 at 19:58
It is also the only way to get an image from the BrowserControl. –  idstam Dec 3 '13 at 17:54
add comment

Is your application exiting after resetting the clipboard?

Assuming it is a Win Form app. (not sure how it works in wpf though) You could use one of the other overloaded version of Clipboard.SetDataObject

public static void SetDataObject(object data, bool copy) 

which preserves the data even after your app exits.

ex: in your case after removing the text content you could call Clipboard.SetDataObject(iDataObject, true);


I Could source step through Clipboard.cs .NET Frameword 4 / VS 2010. Download the .NET Framework 4 from here http://referencesource.microsoft.com/netframework.aspx. Follow the below steps and if it asks for the source (Clipboard.cs) it would be in the Source sub-dir of the installation dir.


Not sure why the same code doesn't work. Cannot be a security/permission issue as the code doesn't throw an exception as you say.

There is another approach - source stepping into Framework code - Clipboard.cs Based on the VS version and .NET framework it may vary ( I couldn't get the source stepping work for .NET 4 as the info is that the symbols with source support haven't yet been released). I'm trying my luck by downloading it manually from here (.NET Version 4)

If you are running VS 2008 and older version of .NET then the below steps should work for you.

Source Symbol Setting

General Debugging Settings

More details are here. For .NET Framework 4 - here

share|improve this answer
The problem is that it has nothing to do with application reset because my application should work a lot of time and the user wouldn't like his clipboard to change. no resetting is going on here. I need to backup & restore while the application is still open. –  user779444 Jun 7 '11 at 9:01
In either case the above pasted code should still work. Does it throw any exception? –  Vijay Sirigiri Jun 7 '11 at 9:07
No, just not working. same as before –  user779444 Jun 7 '11 at 9:15
add comment

Your Answer


