Search code examples
c#winforms.net-2.0clipboard

How to clear the clipboard contents using C#


I have an application where I am using the clipboard for copy and paste operations. For copying I have used this code:

Clipboard.Clear();
const byte VK_CONTROL = 0x11;
keybd_event(VK_CONTROL, 0, 0, 0);
keybd_event(0x43, 0, 0, 0); // Send the C key (43 is "C")
keybd_event(0x43, 0, CONST_KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0, CONST_KEYEVENTF_KEYUP, 0);

But it's giving an error saying Unable to perform the clipboard action, and I am unable to paste it. It's throwing an exception.

How do I fix this issue or are there some other ways to clear the clipboard content before we copy?


Solution

  • I have done it using Win32 API calls (EmptyClipboard function).