How to Backup and Restore the System Clipboard in C#

C# Backing Up And Restoring Clipboard

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

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

Do not use the clipboard unless directly instructed to do so by the logged-on user. It's not for general application storage - it's for the user to use for what they want to clip.

If you want to get text from an arbitrary textbox, locate the window (in the Win32) sense and send an EM_GETTEXT Win32 message.

Save original clipboard with text or image and restore it later in C#

As no one did answer this, then I will do it myself as I did figure out a solution based on @Jimi's comment. My answer does not cover 100% of my question(s) but it solves my problem. I doubt it will work for everything but at least it works for my basic usage of it, which focuses only on texts and images from the clipboard.

I have tested the below code in Windows 10 with Wordpad and Word and it works but of course things can change or work differently in your scenario but maybe it can give some hints for someone with a similar problem :-)

The code will grap relevant formats from the clipboard (formatted texts or images) and save it to a List of Dictionary. Afterwards you can do whatever you want with the clipboard and later you can retrive and restore the clipboard again.

using System.Windows.Forms;
using System.Collections.Generic;

// Define variables
int key = 0;
IDataObject clipboardObject;
SortedList<int, Dictionary<string, object>> clipboardOriginals = new SortedList<int, Dictionary<string, object>>(); // can be referred by integer and can contain clipboard objects

// ---------
// Save formatted clipboard to a list of dictionaries

// Get the clipboard object
clipboardObject = Clipboard.GetDataObject();

// Walk through all (relevant) clipboard formats and save them in a new object
var formats = clipboardObject.GetFormats(false);
Dictionary<string, object> clipboardFormats = new Dictionary<string, object>();
foreach (var format in formats)
{
if (
format.Contains("Text") ||
format.Contains("Hyperlink") ||
format.Contains("Bitmap")
)
{
// Add the clipboard format to the clipboard object
clipboardFormats.Add(format, clipboardObject.GetData(format));
}
}

// Save the clipboard formats to the dictionary containing all originals
clipboardOriginals.Add(key, clipboardFormats);

// ---------
// Now do whatever you want with the clipboard
Clipboard.Clear();
Clipboard.SetText("Cleared");

// ---------
// Restore the (semi) original clipboard - at least the relevant formats we have saved

DataObject data = new DataObject();
foreach (KeyValuePair<string, object> kvp in clipboardOriginals[key])
{
if (kvp.Value != null)
{
data.SetData(kvp.Key, kvp.Value);
}
}

// Copy the saved formats to the clipboard
Clipboard.SetDataObject(data, true);

// Try now to paste the clipboard - it should contain the original formatting (hopefully)? :-)

This may not be bullet-proof at all but so far it seems to work for me and my usage.

C# saving and then retriving data via the clipboard

See this post, it might have what you need:

Strangeness with clipboard access



Related Topics



Leave a reply



Submit