PC Review


Reply
Thread Tools Rate Thread

Copying a File To The Clipboard

 
 
John Smith
Guest
Posts: n/a
 
      22nd Sep 2005
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!!!


 
Reply With Quote
 
 
 
 
Michael Nemtsev
Guest
Posts: n/a
 
      22nd Sep 2005
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


 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      22nd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      22nd Sep 2005
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)
 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      22nd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)



 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      22nd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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)

>
>



 
Reply With Quote
 
=?Utf-8?B?RGhhbnM=?=
Guest
Posts: n/a
 
      23rd Sep 2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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)

> >
> >

>
>
>

 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      23rd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:8D447D1E-9B31-43BC-98A0-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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)
> > >
> > >

> >
> >
> >



 
Reply With Quote
 
=?Utf-8?B?RGhhbnM=?=
Guest
Posts: n/a
 
      26th Sep 2005
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");

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying to the clipboard Jeff Microsoft VB .NET 18 19th Jan 2007 08:50 AM
Clipboard gets empty by itself, cleared clipboard, copy paste doesn't work, outlook clears clipboard, problems with clipboard - possible solution Jens Hoerburger Microsoft Outlook 0 24th Aug 2006 02:44 PM
Copying to clipboard using Ctrl-C or Clipboard.SetText =?Utf-8?B?S2V2aW4gQnVydG9u?= Microsoft Dot NET Framework 0 28th Jul 2006 01:13 AM
Copying the filtered data to clipboard is copying non-visible rows =?Utf-8?B?U2VldGhhUmFtYW4=?= Microsoft Excel Crashes 10 12th Jul 2006 09:39 PM
Re: Copying file to Clipboard fred Microsoft VB .NET 0 5th Apr 2004 12:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:22 AM.