Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Backing Up And Restoring Clipboard

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
Clipboard.SetText("Hello");
//Some stuff that change Cliboard here

Clipboard.SetDataObject(temp);

But it if I copy a text, and run this code, I get nothing on notepad.

NOTE : I can't use Clipboard.Contains because I want to preserve the Clipboard EXACLY how it was before, even if the user copied a file.

like image 739
user779444 Avatar asked Jun 07 '11 08:06

user779444


4 Answers

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)

//Backup
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
Clipboard.SetText("asd");

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

//Restore data
foreach(var lFormat in lFormats)
{
  lDataObject.SetData(lBackup[lFormat]);
}
//This might be unnecessary
Clipboard.SetDataObject(lDataObject);
like image 69
Andre Luus Avatar answered Sep 18 '22 13:09

Andre Luus


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);

EDIT:2

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.

EDIT:1

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

like image 28
Vijay Sirigiri Avatar answered Sep 17 '22 13:09

Vijay Sirigiri


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

like image 20
Chris Thornton Avatar answered Sep 17 '22 13:09

Chris Thornton


I tested the pseudocode from Lukas and found out doesn't work always, this works in all my tests:

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

//Set test data
Clipboard.SetText("asd");

//Restore clipboard
lDataObject = new DataObject();
foreach (var lFormat in lFormats)
{
    lDataObject.SetData(lFormat, lBackup[lFormat]);
}
//This might be unnecessary
Clipboard.SetDataObject(lDataObject);
like image 32
Karsten Avatar answered Sep 21 '22 13:09

Karsten