如何从剪贴板检索数据作为 System.String[]
当我从应用程序复制数据时,我编写了一个简单的 C# 脚本来检查它的类型。显然(并且我期望如此),它是一个字符串数组:
IDataObject data = Clipboard.GetDataObject();
Console.WriteLine(data.GetFormats(true)); // writes "System.String[]"
现在,当我提取数据时,
object o = data.GetData( "System.String[]" );
结果对象保持为空。
为什么?我该如何提取数据?
When I copy data from my application, I wrote a simple C# script to check what type it is. Apparently (and I expected that), it's an array of strings:
IDataObject data = Clipboard.GetDataObject();
Console.WriteLine(data.GetFormats(true)); // writes "System.String[]"
Now when I extract the data like
object o = data.GetData( "System.String[]" );
the resulting object stays null.
Why? How am I to extract the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该将 CLR 类型作为参数。 GetData 的参数只是一个标识符,可以是任何内容,但有一些 预定义格式。
您可能想要做的是使用 DataFormats.Text 以文本形式(即字符串)检索数据。请注意,只有当剪贴板内容的源实际提供这种格式的数据时,这才有效,但大多数这样做应该是安全的。
而且,由于文本是一种常见的格式,因此甚至有一种方便的方法可以以该格式检索文本,称为 Clipboard.GetText()
编辑:调用 GetFormats 时返回的 string[] 只是列出所有可用格式的字符串数组。它不是实际的剪贴板数据,它只是告诉您在执行
obj.GetData()
时可以获得哪种格式。在调试器中查看该数组或在 foreach 中打印它以查看是否有任何类似于数组的格式。You are not supposed to put the CLR types as parameters. The parameter to GetData is just an identifier that can be anything, but there are some pre-defined formats which many programs use.
What you probably want to do is use DataFormats.Text to retrieve the data in text form (i.e. a string). Note that this only works if the source of the clipboard contents actually provided data in this format, but most do so you should be safe.
And, since text is such a common format, there's even a convenience method to retrieve it in that format called Clipboard.GetText()
EDIT: The string[] you get back when you call GetFormats is just an array of strings listing all the available formats. It's not the actual clipboard data, it just tells you which format you can get it in when you do
obj.GetData()
. Look at that array in the debugger or print it in a foreach to see if there's any format that is array-like.data.GetFormats(true)
作者:MSDN返回存储在剪贴板内的数据格式的名称以及剪贴板中这些格式可以转换为的所有数据格式。要获取数据,您需要调用您想要获取的数据格式的data.GetData(dataFormatName)
。如果你想获取所有对象,你应该这样做:data.GetFormats(true)
by MSDN returns names of data formats that are stored inside the clipboard along with all data formats that those formats in clipboard can be converted to. To get data you need to calldata.GetData(dataFormatName)
of data format you want to get. If you want to get all the objects you should do this: