开发者

C# saving and then retriving data via the clipboard

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?

开发者_Go百科

Is there a way to do this?

Thanks.


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.


See this post, it might have what you need:

Strangeness with clipboard access

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜