Ketone Cops

July 31, 2008

Using the Clipboard to copy and paste files in ASP.NET

Filed under: ASP.NET 2.0,programming — delroger @ 9:27 pm

Before I start, I must admit I’m not entirely convinced this is a particularly useful thing to try and do, since you can accomplish the same results using System.IO and the various File methods, but anyway…

I guess I was trying to see if this was possible or not in a webpage, rather than in a Windows form – to copy a file (or files) to the server clipboard and then paste them in another location. So here’s how it’s done:

Let’s assume you have a Gridview where you have a list of files that the user can access, and they want to move them from one directory to another. They can tick a checkbox in each gridview row for the file, then click a button to ‘Copy Files’ and another button to ‘Paste Files’ to another directory.

In the code-behind, we have a Property set up called ClipboardFiles which uses a string array as its value, like this:

Property ClipboardFiles() As String()
        Get
            Dim o As Object = ViewState(“ClipboardFiles”)
            If o Is Nothing Then
                Return Nothing
            Else
                Return o
            End If
        End Get
        Set(ByVal value As String())
            ViewState(“ClipboardFiles”) = value
        End Set
    End Property

Now when the user clicks the button to Copy Files, all selected files are added to ClipboardFiles. Of course there are lots of ways you might get the names and paths of the files; in this case, I had a function that was returning an ArrayList (named ‘al’ here) of the complete path and name of the files. I then cast the ArrayList to a string array and set the ClipboardFiles property like this:

Dim strFiles As String() = CType(al.ToArray(GetType(String)), String()) 
ClipboardFiles = strFiles

Here’s the tricky bit: when the user clicks the button to Paste Files, we have to use Threading and set the Apartment State of the thread. There is a sub called PasteFiles, which we will reference in the button click, like this:

(note: needs Imports System.Threading)

Protected Sub imgPasteFile_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPasteFile.Click
        Dim pasteThread As Thread = New Thread(New ThreadStart(AddressOf PasteFiles))
        pasteThread.SetApartmentState(Threading.ApartmentState.STA)
        pasteThread.Start()
        pasteThread.Join()
    End Sub

Finally, the PasteFiles sub looks like the code below. It copies the files listed in the ClipboardFiles to the Clipboard, then pastes them in a location of your choice (in this case ‘C:\Temp’, but you’d almost certainly set this programmatically).

(note: needs Imports System.Windows.Forms)

Private Sub PasteFiles()
        ‘copy files to clipboard
        If Not ClipboardFiles Is Nothing Then
            Try
                Clipboard.SetDataObject(New DataObject(DataFormats.FileDrop, ClipboardFiles))
            Catch ex As Exception
                ‘your error handler
                Exit Sub
            End Try
        End If

        ‘paste to new location
        Try
            Dim iData As IDataObject = Clipboard.GetDataObject()
            If iData.GetDataPresent(DataFormats.FileDrop) Then
                Dim clipbrd As String() = DirectCast(iData.GetData(DataFormats.FileDrop), String())
                For Each strFile As String In clipbrd
                    System.IO.File.Copy(strFile, “C:\Temp\” & System.IO.Path.GetFileName(strFile))
                Next
            End If
        Catch ex As Exception
            ‘your error handler
        End Try
       
    End Sub

This code was put together from a few snippets I found here and there, including this:

http://www.telerik.com/community/forums/thread/b311D-dcdga.aspx

and this:

http://bytes.com/forum/thread356140.html

Like I said, I’m not entirely sure how useful it really is, but it may give someone an idea about how to accomplish something similar using the Clipboard functionality. If so, glad to have helped…

Advertisements

1 Comment »

  1. Hello,

    Nice to be registered on delroger.wordpress.com. My little name is maxizhu 😉

    Comment by zhuzhu — January 11, 2011 @ 2:44 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: