How to Add Format to Existing Clipboard Contents in C#

Asked 2 years ago, Updated 2 years ago, 44 views

Thank you for your help.
Sorry for asking so many questions.
You are about to add a new format to the contents of the clipboard that is already included in C#.
For example, an image in which a file is copied to the clipboard and data such as strings are added to it.
Below is the data from the clipboard and I'm trying to add a string to it.

using System.Windows.Forms;

IDataObject data=Clipboard.GetDataObject();
if(data==null){
return;
} else {
data.SetData(DataFormats.Text, content);
Clipboard.SetDataObject(data,true);
}

However, it doesn't seem to work well with this code, and rather than the string being copied, it seems that the contents of the previous copy have disappeared.
When you copy the file and look at the format stored on the clipboard, you will see the following:

 FileDrop, FileNameW, FileName, Preferred DropEffect

After that, if you add string data with the above code, the format list is as follows, but if you actually try to paste a string or a file, you cannot paste it.

 FileDrop, FileNameW, FileName, Preferred DropEffect

Could you tell me what the problem is with the above code?
Thank you for your cooperation.

c#

2022-09-30 20:17

1 Answers

The clipboard is exactly called OLE Clipboard, and it is realized by Paste the IDataObject provided by the source.
IDataObject (System.Runtime.InteropServices.ComTypes.IDataObject) The runtime wrapped IDataObject (https://docs.microsoft.com/ja-jp)"/dotnet/api/system.windows.forms.idataobject?view=netframework-4.8"rel="nofollow noforeerer">System.Windows.Forms.IDataObject) is used.

The code mentioned in the question is only for objects wrapped in their own process.Also, as mentioned earlier, the source and the pasting destination are negotiated through IDataObject, so no third party can intervene and should not be done.In the worst case scenario, it could be considered a virus or other intrusion.


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.