简体   繁体   中英

How would I save an RTF file from clipboard in C#?

so I'm attempting to dump some RTF from the clipboard to a file.

Essentially, what's happening is that if the application see's that the user has RTF in the clipboard when they paste, it dumps that RTF to a file that is specified earlier.

The code that I was trying to use to do this is as follows:

private void saveTextLocal(bool plainText = true)
{
    object clipboardGetData = Clipboard.GetData(DataFormats.Rtf);
    string fileName = filename();
    using (FileStream fs = File.Create(fileLoc)) { };
    File.WriteAllBytes(fileLoc, ObjectToByteArray(clipboardGetData));
}
private byte[] ObjectToByteArray(Object obj)
{
    if (obj == null)
    {
        return null;
    }
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
}

This appears to almost work, producing the following information as the file:

    ÿÿÿÿ          ‰{\rtf1\ansi\deff0\deftab480

{\fonttbl
{\f000 Courier New;}
{\f001 Courier New;}
{\f002 Courier New;}
{\f003 Courier New;}
}

{\colortbl
\red128\green128\blue128;
\red255\green255\blue255;
\red000\green000\blue128;
\red255\green255\blue255;
\red000\green000\blue000;
\red255\green255\blue255;
\red000\green000\blue000;
\red255\green255\blue255;
}

\f0\fs20\cb7\cf6 \highlight5\cf4 Console\highlight3\cf2\b .\highlight5\cf4\b0 WriteLine\highlight3\cf2\b (\highlight1\cf0\b0 "pie!"\highlight3\cf2\b )}

Which does appear to be almost right. Opening the file I'm copying in Notepad++ looks like this:

{\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Courier New;}}
{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue128;\red128\green128\blue128;}
{\*\generator Riched20 6.2.9200}\viewkind4\uc1 
\pard\cf1\highlight2\f0\fs20\lang2057 Console\cf3\b .\cf1\b0 WriteLine\cf3\b (\cf4\b0 "pie!"\cf3\b )\cf1\b0\par
}

Did I do something obviously wrong, and if so - how would I amend my code to fix it?

Thanks in advance!

The issue was, as madamission quite rightly pointed out, that RTF is ASCII - not binary, and thus running it through a binary converter was wholly the wrong direction.

Instead, I did a cast of the clipboard data object to get it into a string, and I wrote as you would for a normal text file. This produced the file I was expecting. The following is the working code for anyone who might find this:

private void saveTextLocal(bool plainText = true)
{
    //First, cast the clipboard contents to string. Remember to specify DataFormat!
    string clipboardGetData = (string)Clipboard.GetData(DataFormats.Rtf);

    //This is irrelevant to the question, in my method it generates a unique filename
    string fileName = filename();

    //Start a StreamWriter pointed at the destination file
    using (StreamWriter writer = File.CreateText(filePath + ".rtf"))
    {
        //Write the entirety of the clipboard to that file
        writer.Write(clipboardGetData);
    };
    //Close the StreamReader
}

RTF is only ASCII I think and not binary so I think you should use a TextWriter instead and don't use the BinaryFormatter.

There are some related solutions here: How to create RTF from plain text (or string) in C#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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