dotNET
dotNET

Reputation: 35400

Setting clipboard contents to custom data

I'm copying some XML to Clipboard that makes sense to my app only and I do not want other applications to be able to paste. Using the following line:

Clipboard.SetData(DataFormats.UnicodeText, myXML);

I can copy and paste thing correctly, but other text editors can paste it as well. I was expecting there would be a DataFormats.Custom for this situation but there isn't.

Is there a way to send data to Clipboard that is available to my app only?

(I'm assuming that applications check available DataFormats before fetching clipboard content and do not fetch them if the returned format is not something they can handle. For example Notepad would do nothing if I use Paste command after copying an image)

Upvotes: 2

Views: 1623

Answers (1)

Evk
Evk

Reputation: 101473

Clipboard.SetData expects simple string as first argument, DataFormat is not an enum. So you can do just this:

Clipboard.SetData("my_custom_format", myXml);

Then you can check if clipboard contains your data with

if (Clipboard.ContainsData("my_custom_format")) {
    var xml = Clipboard.GetData("my_custom_format");
}

And you won't be able to paste this data into other editors like Notepad.

Upvotes: 3

Related Questions