By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,706 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,706 IT Pros & Developers. It's quick & easy.

Copying a File To The Clipboard

P: n/a
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!!!
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Nov 17 '05 #2

P: n/a
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...).

"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:43***************************@msnews.microsof t.com...
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

Nov 17 '05 #3

P: n/a
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
VB.NET. Here it is: http://www.codeproject.com/useritems...eeDragDrop.asp
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #4

P: n/a
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.
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
John Smith wrote:
Does anyone have any sample code for how to copy the file C:\test.txt intothe 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...eeDragDrop.asp

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #5

P: n/a
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" <m@ty.com> wrote in message
news:uI**************@TK2MSFTNGP15.phx.gbl...
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.
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
John Smith wrote:
Does anyone have any sample code for how to copy the file C:\test.txt intothe 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...eeDragDrop.asp


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)


Nov 17 '05 #6

P: n/a
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" wrote:
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" <m@ty.com> wrote in message
news:uI**************@TK2MSFTNGP15.phx.gbl...
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.
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
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 VB.NET. Here it is:

http://www.codeproject.com/useritems...eeDragDrop.asp


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)



Nov 17 '05 #7

P: n/a
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" <Dh***@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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" wrote:
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" <m@ty.com> wrote in message
news:uI**************@TK2MSFTNGP15.phx.gbl...
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.
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
> 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
> VB.NET. Here it is:
http://www.codeproject.com/useritems...eeDragDrop.asp
>
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)


Nov 17 '05 #8

P: n/a
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");

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.