3

Well the question is that. Is there anyway to copy text and/or files to the Windows clipboard WITHOUT using Clipboard Class from .NET?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
wencha
  • 157
  • 1
  • 2
  • 11
  • Why do you want to accomplish this? There are various ways, including WPF Clipboard class, and directly interacting with the Clipboard API...but it would help to know your motivation... – Jeff Sep 27 '11 at 17:50
  • Sure...Interface directly with the Windows Clipboard which is exactly what the Clipboard class does. @JeffN825 - What....The WPF Clipboard per the user cannot be used since its the "Clipboard Class from .NET" – Security Hound Sep 27 '11 at 17:50
  • Why bypass the built in functionality? – The Evil Greebo Sep 27 '11 at 17:50
  • 2
    You have to say what is wrong with the built-in class so that you get a solution that doesn't have the same problem. – Gabe Sep 27 '11 at 17:51
  • @Ramhound: The WPF Clipboard and the Windows Forms Clipboard are two different classes. – Jeff Sep 27 '11 at 17:59
  • @JeffN825 - Looking at the MSDN Support Article they look nearly identical and interface with the system clipboard. – Security Hound Sep 27 '11 at 18:24
  • @Ramhound: their implementation may be similar but they are definitely different. I have disassembled both in the past and the WPF clipboard is more resilient and handles certain image formats on the clipboard better. They do both talk to the same OLE Clipboard. – Jeff Sep 27 '11 at 19:37
  • To demonstrate for yourself: copy an image from MS Excel 2003 or older and see what you get on the WPF clipboard vs the Windows Forms clipboard. – Jeff Sep 27 '11 at 19:39
  • You should be able to import the relevant native API functions and call those, but why would you? – 500 - Internal Server Error Sep 27 '11 at 17:51

1 Answers1

5

Depends on if I am answering the question in the title or in the text... you can access System.Windows.Clipboard without having access to System.Windows.Forms...

string textData = "I want to put this string on the clipboard.";

// After this call, the data (string) is placed on the clipboard and tagged
// with a data format of "Text".
System.Windows.Clipboard.SetData(DataFormats.Text, (Object)textData);
therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36
  • @JeffN825 - Its basically the exact samething, different dlls, just modified to support Presentation Framework. – Security Hound Sep 27 '11 at 18:23
  • No, it's not. They are different. – Jeff Sep 27 '11 at 19:38
  • This is correct. But it requires you to load PresentationCore.dll, which is quite a beast. if you want to go hard-core, consider: https://www.pinvoke.net/default.aspx/user32/SetClipboardData.html – cskwg Sep 20 '20 at 05:55
  • cskwg's link caused a heap corruption when I called it more than once, not sure if that issue was exclusive to me but this worked for me instead: https://stackoverflow.com/a/1264179/11321886 – BBonless Dec 16 '22 at 03:55