如何以System.String []的形式从剪贴板检索数据 [英] How to retrieve data from clipboard as System.String[]

查看:40
本文介绍了如何以System.String []的形式从剪贴板检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从应用程序中复制数据时,我编写了一个简单的C#脚本来检查其类型。显然(而且我期望如此),它是一个字符串数组:

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[]"

现在,当我提取数据时

      object o = data.GetData( "System.String[]" );

结果对象保持为空。

为什么?我该如何提取数据?

Why? How am I to extract the data?

推荐答案

不应该将CLR类型作为参数。 GetData的参数只是一个标识符,可以是任何标识符,但是有一些许多程序使用的预定义格式

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.

您可能想使用的是 DataFormats.Text 以文本形式(即字符串)检索数据。请注意,这仅在剪贴板内容的源实际以这种格式提供数据时才有效,但是大多数这样做都应该是安全的。

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.

而且,因为文本是如此常见格式,甚至还有一种方便的方法可以以 Clipboard.GetText( )

And, since text is such a common format, there's even a convenience method to retrieve it in that format called Clipboard.GetText()

编辑:调用GetFormats时返回的string []只是列出所有可用格式的字符串数组。这不是实际的剪贴板数据,它只是告诉您在执行 obj.GetData()时可以使用哪种格式。在调试器中查看该数组或在foreach中打印该数组,以查看是否存在类似于数组的格式。

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.

这篇关于如何以System.String []的形式从剪贴板检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆