C# 通过剪贴板保存然后检索数据
好的,
我有一个程序可以自动将数据复制到剪贴板,然后复制到内存中。我想要做的是,在程序运行之前保存剪贴板上的数据,然后在完成需要执行的操作后将其复制回剪贴板。
我可以这样从剪贴板获取数据:
IDataObject currentClipboard = Clipboard.GetDataObject();
但是,当我想将数据放回剪贴板时,我是否需要知道该数据的格式才能进行转换?
有办法做到这一点吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法可靠地将剪贴板恢复到以前的状态。时期。使用简单的格式,您可能会侥幸逃脱。当您使用更复杂的格式时,尤其是 OLE 和私有格式,情况会复杂得多。阅读延迟渲染,您会意识到数据还不一定在剪贴板上。因此,当您请求数据以便将其存储起来时,您将导致长时间的延迟。考虑 Excel 中的 5000 个单元格,它们可以呈现为大约 24 种不同的格式,其中大多数都使用延迟呈现(这意味着它们并不真正位于剪贴板上,直到您请求特定格式的数据,然后 Excel 必须生成它按需)。
即使可以,每次恢复数据时,您都会生成一个剪贴板更新事件,该事件将发送给所有注册的剪贴板查看器,这将对您的恶作剧做出反应。
总之:不要这样做。
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