Reputation: 10738
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
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);
}
Upvotes: 1
Views: 1694
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