fs_tigre
fs_tigre

Reputation: 10738

How to find and copy .pdf file to clipboard

I'm trying to copy a pdf file to the clipboard so I can later paste it by Ctrl+V.

The following code can find a file but I'm not sure how to copy it to the clipboard.

How can I copy a pdf file once it is found?

    private void copyToClipbard_Click(object sender, RoutedEventArgs e)
    {
        var file = Directory.GetFiles(@"C:\MyFolder\", "myPDFFile.pdf", SearchOption.AllDirectories).FirstOrDefault();
        Clipboard.SetDataObject(file);
    }

I get error:

An unhandled exception of type 'System.ArgumentNullException' occurred in PresentationCore.dll

EDIT: I also tried the following, I don't get any errors but it doesn't copy the file.

EDIT: The following code does work, I was not putting the right path, I like jasttims answer better though.

    private void copyToClipbard_Click(object sender, RoutedEventArgs e)
    {
        var file = Directory.GetFiles(@"C:\MyFolder\", "myPDFFile.pdf", SearchOption.AllDirectories).FirstOrDefault();

        var dataObj = new DataObject();
        string[] fileName = new string[1];
        fileName[0] = file;
        dataObj.SetData(DataFormats.FileDrop, fileName, true);

        Clipboard.SetDataObject(dataObj, true);
    }

Upvotes: 1

Views: 1694

Answers (1)

jasttim
jasttim

Reputation: 782

Try using the "Clipboard" class.
It features all the methods necessary for putting data on the Windows clipboard.

StringCollection paths = new StringCollection();
paths.Add("c:\file.pdf");
Clipboard.SetFileDropList(paths);

Upvotes: 2

Related Questions