Copying a File To The Clipboard

J

John Smith

Hi folks,

I know how to place text into the user's clipboard:
Clipboard.SetDataObject("My Copied Text");
but how do I place a file in there?

So, if I have a file C:\test.txt, how can I place that file into the user's
clipboard so that they can later paste it into explorer?

Thanks!!!
 
M

Michael Nemtsev

Hello John,

I dont remember the author who wrote pretty nice about clipboard, but the
general idea is u can specify the format
for Clipboard and describe how to treat and save some types of data. For
example copy/paste of picture performing directly through clipboad.
But files use in other way. U just keep a path to file and when pasting u
perform copying

JS> I know how to place text into the user's clipboard:
JS> Clipboard.SetDataObject("My Copied Text");
JS> but how do I place a file in there?
JS> So, if I have a file C:\test.txt, how can I place that file into the
JS> user's clipboard so that they can later paste it into explorer?
JS>
JS> Thanks!!!
JS>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

John Smith

Thanks, but I'm looking for some sample code because I've been trying to get
this working for a while now and did a bunch of searching on the topic all
with no success.

Does anyone have any sample code for how to copy the file C:\test.txt into
the user's clipboard? I need to support multiple filetypes (like pdf, doc,
zip...).
 
O

Oliver Sturm

John said:
Does anyone have any sample code for how to copy the file C:\test.txt into
the user's clipboard? I need to support multiple filetypes (like pdf, doc,
zip...).

You don't really copy the file to the clipboard, but rather a reference to
it in the form of a PIDL (a Windows Shell id). You also need to adhere to
a specific format called "Shell IDList Array". I searched Google for it
and found a nice CodeProject article that shows something quite similar in
VB.NET. Here it is: http://www.codeproject.com/useritems/ExpTreeDragDrop.asp


Oliver Sturm
 
J

John Smith

Sorry....no go. I need a real sample of copying a file to the clipboard.
Seems like the only file that's easy to copy is a bitmap image.
Anyone got one? It seems like it's never been done before. I've been
researching it for hours.
 
J

John Smith

Finally got it and it's pretty easy. I was trying this earlier and it
wasn't working, but that's because I didn't have the file name as an array
(how annoying!):

DataObject objData = new DataObject();
string []filename = new string[1];
filename[0] = "c:\\testFile.exe";
objData.SetData(DataFormats.FileDrop, true, filename);
Clipboard.SetDataObject(objData, true);



John Smith said:
Sorry....no go. I need a real sample of copying a file to the clipboard.
Seems like the only file that's easy to copy is a bitmap image.
Anyone got one? It seems like it's never been done before. I've been
researching it for hours.
 
G

Guest

Smith,
Similar problem i am facing, thanks for your code snippet for file
copying to clipboard.
Now, how do I get back that file from clipboard or paste in some other
location.

Thanks,
Dhans

John Smith said:
Finally got it and it's pretty easy. I was trying this earlier and it
wasn't working, but that's because I didn't have the file name as an array
(how annoying!):

DataObject objData = new DataObject();
string []filename = new string[1];
filename[0] = "c:\\testFile.exe";
objData.SetData(DataFormats.FileDrop, true, filename);
Clipboard.SetDataObject(objData, true);
 
J

John Smith

I haven't actually done it because it's not in my project scope, but it
shouldn't be too hard from what I've seen. You basically just do the
reverse. This is completely untested code, and will just get you started,
but it should go a little something like this:

IDataObject objData2 = Clipboard.GetDataObject();
if(objData2.GetDataPresent(DataFormats.FileDrop, true)) // Check to see if
the data is in the clipboard
{
//Create file using something like:
//myFile = objData2.GetData(DataFormats.FileDrop, true);
//and then write it out using the System.IO namespace classes
}

When you figure it out, post it here for the future frustrated programmer to
see :)




Dhans said:
Smith,
Similar problem i am facing, thanks for your code snippet for file
copying to clipboard.
Now, how do I get back that file from clipboard or paste in some other
location.

Thanks,
Dhans

John Smith said:
Finally got it and it's pretty easy. I was trying this earlier and it
wasn't working, but that's because I didn't have the file name as an array
(how annoying!):

DataObject objData = new DataObject();
string []filename = new string[1];
filename[0] = "c:\\testFile.exe";
objData.SetData(DataFormats.FileDrop, true, filename);
Clipboard.SetDataObject(objData, true);



John Smith said:
Sorry....no go. I need a real sample of copying a file to the clipboard.
Seems like the only file that's easy to copy is a bitmap image.
Anyone got one? It seems like it's never been done before. I've been
researching it for hours.


John Smith wrote:

Does anyone have any sample code for how to copy the file C:\test.txt
into
the user's clipboard? I need to support multiple filetypes (like pdf,
doc,
zip...).

You don't really copy the file to the clipboard, but rather a
reference
to
it in the form of a PIDL (a Windows Shell id). You also need to
adhere
to
a specific format called "Shell IDList Array". I searched Google for it
and found a nice CodeProject article that shows something quite
similar
in
 
G

Guest

Thanks Smith,
I can get the name of the files which are copied into clipboard, here is the
code snippet. May be its not 100% correct code... but it works for me

object fromClipboard;
IDataObject fileData = Clipboard.GetDataObject();
if( fileData.GetDataPresent(DataFormats.FileDrop, true) )
{
//it returning array of filenames present inside clipboard
fromClipboard = fileData.GetData(DataFormats.FileDrop, true);

foreach( string sourceFileName in (Array)fromClipboard)
MessageBox.Show(sourceFileName);
}
else
MessageBox.Show(this, "File not present in clipboard");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top