简体   繁体   English

使用Clipboard.SetText复制空字符串(字符串)

[英]Copy empty string using Clipboard.SetText(string)

Clipboard.SetText("") throws me an error - "Value cannot be null". Clipboard.SetText("")抛出一个错误 - “值不能为空”。 So how do I copy an empty string using Clipboard.SetText ? 那么如何使用Clipboard.SetText复制空字符串?

I have already done Clipboard.Clear() . 我已经完成了Clipboard.Clear() It does clear the clipboard, but it doesn't help me to paste an empty string 它确实清除了剪贴板,但它无法帮助我粘贴一个空字符串

Any suggestions? 有什么建议?

If you try to save null or an empty string using Clipboard.SetText it will never work. 如果您尝试使用Clipboard.SetText保存null或空字符串,它将永远不会工作。

See Clipboard.SetText Method (String) (MSDN). 请参见Clipboard.SetText方法(字符串) (MSDN)。 It mentions ArgumentNullException is thrown if the text is null or Empty for Clipboard.SetText . 它提到如果文本为null或者为Clipboard.SetTextEmpty ,则抛出ArgumentNullException。

Hence you cannot achieve what you are trying to achieve. 因此,你无法实现你想要实现的目标。

I think you need to do 我想你需要这样做

Clipboard.Clear()

From MSDN 来自MSDN

Clears any data from the system Clipboard. 清除系统剪贴板中的所有数据。

See Clipboard.Clear Method (System.Windows.Forms) (MSDN). 请参见Clipboard.Clear方法(System.Windows.Forms) (MSDN)。

Clipboard.Clear();

will clear the clipboard, so you will "paste" an empty string. 将清除剪贴板,因此您将“粘贴”一个空字符串。

Reference PresentationCore and call System.Windows.Clipboard.SetText(string) instead of System.Windows.Forms.Clipboard.SetText(string) . 引用PresentationCore并调用System.Windows.Clipboard.SetText(string)而不是System.Windows.Forms.Clipboard.SetText(string) I used clipview to verify that System.Windows.Clipboard.SetText(""); 我用clipview来验证System.Windows.Clipboard.SetText(""); (which throws no exception) stores the empty string into the clipboard. (不会引发异常)将空字符串存储到剪贴板中。

This method it saves you from having to conditionally call Clear() or SetText() depending on what string you are trying to effectively set into clipboard. 这种方法可以使您不必有条件地调用Clear()SetText()具体取决于您尝试有效设置到剪贴板的字符串。 This is especially useful if you're trying to write code to set the clipboard to any string value without knowing ahead of time whether or not the empty string needs to be supported. 如果您尝试编写代码以将剪贴板设置为任何字符串值,而不事先知道是否需要支持空字符串,则此功能尤其有用。 Ie, this method enables you to avoid treating the empty string like a special case (IMO, it shouldn't be a special case because that's just confusing). 也就是说,这种方法可以避免像处理特殊情况一样处理空字符串(IMO,它不应该是特殊情况,因为这只是令人困惑)。

Additionally, as can be seen with clipview, Clear() actually empties out the clipboard instead of placing an empty string in it. 另外,正如clipview所见, Clear()实际上清空了剪贴板而不是在其中放置一个空字符串。 Calling System.Windows.Clipboard.SetText("") actually puts an empty string value into clipboard. 调用System.Windows.Clipboard.SetText("")实际上将空字符串值放入剪贴板。 When you paste, the target application can tell the difference and actually behave differently if it wants to. 粘贴时,目标应用程序可以分辨出差异,并且如果需要,实际上会有不同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM