题
我想要写HTML格式,但我甚至不能得到它的一个简单的例子MSDN工作。
http://msdn.microsoft.com/en-us/library /tbfb3z56.aspx
这是否控制台应用程序,剪贴板轮卸料,工作的人?
using System; using System.Windows; //Need to add a PresentationCore or System.Windows.Forms reference class Program { [STAThread] static void Main( string[] args ) { Console.WriteLine( "Copy a small amount of text from a browser, then press enter." ); Console.ReadLine(); var text = Clipboard.GetText(); Console.WriteLine(); Console.WriteLine( "--->The clipboard as Text:" ); Console.WriteLine( text ); Console.WriteLine(); Console.WriteLine( "--->Rewriting clipboard with the same CF_HTML data." ); //***Here is the problem code*** var html = Clipboard.GetText( TextDataFormat.Html ); Clipboard.Clear(); Clipboard.SetText( html, TextDataFormat.Html ); var text2 = Clipboard.GetText(); Console.WriteLine(); Console.WriteLine( "--->The clipboard as Text:" ); Console.WriteLine( text2 ); var isSameText = ( text == text2 ); Console.WriteLine(); Console.WriteLine( isSameText ? "Success" : "Failure" ); Console.WriteLine(); Console.WriteLine( "Press enter to exit." ); Console.ReadLine(); } }
解决方案
当从浏览器的数据复制到剪贴板,它把同样的数据到剪贴板上以多种格式,包括文本和HTML。所以,你可以读取数据文本或HTML格式的退了出去。但是,当你在这里打电话的setText,你只有经过HTML格式,所以当您使用常规的getText,有剪贴板上没有文字的版本,你会得到空回来。
您可以把多种格式复制到剪贴板上一次使用的IDataObject(即文本和HTML),但你有你把剪贴板上的数据之前自己做格式之间的转换。还有如何使用的例子IDataObject的这里。
其他提示
我可以重现它不工作...的var text2 = Clipboard.GetText();
回报每次""
...
(编辑) 快速搜索产量这个,这似乎上的主题。
不隶属于 StackOverflow