Unable to round html format to clipboard

I want to write Html format, but I can't even get a simple MSDN example for it to work.

http://msdn.microsoft.com/en-us/library/tbfb3z56.aspx

Is this a console app, round clipboard switcher, used by anyone?

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 ();
    }
}
+1


source to share


2 answers


When you copy data from the browser to the clipboard, it puts the same data on the clipboard in multiple formats, including text and HTML. This way you can read data in text or HTML format. However, when you call SetText here you are ONLY transmitting in HTML, so when you use regular GetText there is no text version on the clipboard and you return zero.



You can put multiple formats on the clipboard at the same time (like text and HTML) using IDataObject, but you need to translate between the formats before sending data to the clipboard. Here's an example on how to use IDataObject here .

+2


source


I can reproduce that it doesn't work ... var text2 = Clipboard.GetText();

returns ""

every time ...



(edit) A quick search gives this one , which seems to be related.

0


source







All Articles