C# saving and then retriving data via the clipboard

放肆的年华 提交于 2019-12-08 02:56:42

问题


Okay,

So I have a program that automatically copies data to the clipboard and then into memory. What I want to do is, to save the data that is on the clipboard prior to my program running and then to copyit back to the clipboard once I have finished what I need to do.

I can get the data fromt he clipboard as such:

IDataObject currentClipboard = Clipboard.GetDataObject();

However, when I want to put the data back on to the clipboard, do I need to know what format that data is in to cast it?

Is there a way to do this?

Thanks.


回答1:


See this post, it might have what you need:

Strangeness with clipboard access




回答2:


You cannot reliably restore the clipboard to its former state. Period. With simple formats, you may get away with it. When you get into more complex formats, particularly OLE and private formats, it's much more complicated. Read up on Delayed Rendering, and you'll realize the data isn't necessarily even ON THE CLIPBOARD YET. So when you request the data, so you can store it away, you're going to cause lengthy delays. Consider 5000 cells from Excel, which can be rendered into about 24 different formats, most of which are using delayed rendering (meaning that they're not really on the clipboard until you request data in a particular format, and then Excel has to generate it on-demand).
And even if you could, every time you restore the data, you're going to generate a clipboard update event that is going to be sent to all registered clipboard viewers, which will react to your shennanegans. In summary: Don't do it.



来源:https://stackoverflow.com/questions/4735559/c-sharp-saving-and-then-retriving-data-via-the-clipboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!