简体   繁体   中英

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 jasttim s 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);
    }

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);

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