Paste files from the clipboard in C#

[paste files]

It’s a bit harder to paste files from the clipboard than it is to paste text. Copying and pasting text is relatively straightforward. See Copy and paste text to and from the clipboard C#.

When you copy files to the clipboard, the data copied is actually an array of strings holding the names of the files. This example shows how you can paste those files from the clipboard.

At design time, I set the form’s KeyPreview property to true so the form receives keyboard events before its controls do. The following KeyDown event handler looks for Ctrl+V events and pastes files.

// Handle paste events.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    // Look for Ctrl+V.
    if (e.Control && (e.KeyCode == Keys.V))
    {
        // Paste.
        // Clear the ListBox.
        lstFiles.Items.Clear();

        // Get the DataObject.
        IDataObject data_object = Clipboard.GetDataObject();

        // Look for a file drop.
        if (data_object.GetDataPresent(DataFormats.FileDrop))
        {
            string[] files = (string[])
                data_object.GetData(DataFormats.FileDrop);
            foreach (string file_name in files)
            {
                string name = file_name;
                if (System.IO.Directory.Exists(file_name))
                    name = "[" + name + "]";
                lstFiles.Items.Add(name);
            }
        }
    }
}

The code looks for a control key together with the V key. If it finds that, then it clears the form’s ListBox. It then gets the Clipboard object’s data object and uses that object’s GetDataPresent method to see if a file drop is present.

If there is a FileDrop present, the program uses the GetData method to get the data. The result is a generic object, so the code casts it into an array of strings.

Finally, the method loops through the array of file names, displaying them in the form’s ListBox.

If you look closely at the picture, you’ll see that the project’s bin, obj, and Properties subdirectories are included in the list. There’s no indication in the clipboard’s data that some of the “files” in the file drop are actually directories. You just get a list of names.

The program uses the System.IO.Directory.Exists method to test each file to see if it is actually a directory. If a file is a directory, the program adds [ brackets ] around its name before adding it to the ListBox.

In an actual program, you’ll need to decide what to do with the files that are pasted into your application. For example, you might copy them into a backup directory, display their text in a TextBox, or update their last modification times.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in clipboard, interoperability and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Paste files from the clipboard in C#

  1. Pingback: Copy files to the clipboard in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *