Paste files from the clipboard

Thanks to the problem Copy the files to the clipboard in C # , I was able to use Clipboard.SetFileDropList and as a result:

using System; using System.Collections.Specialized; using System.Windows.Forms; class Program { [STAThread] static void Main ( string[] args) { StringCollection paths = new StringCollection(); paths.Add( @"C:\Users\Antonio\Desktop\MyDirectory" ); Clipboard.SetFileDropList( paths); } } 

That way I can place the entire folder on the clipboard and paste it where I need it. I would like to be able to insert it with code. I don't want to go where I want to paste it, and then press Ctrl + V. In other words, I'm looking for something like:

 Clipboard.Paste("C:\Users\LocationWhereIWantToPasteTheFolder") 

I know that I can retrieve all files recursively and then insert them one by one. But why reinvent the wheel? It would be nice if the OS can do this for me ...

+4
source share
1 answer

The clipboard has a protocol, a mutually agreed way to get data from one process to another. Such a protocol should have limited ability to put reasonable data on the clipboard. You can put whatever you want on the clipboard, especially the .NET object. But if another application pasting data into the clipboard does not understand .NET objects, it is very likely that if it was not written in .NET, then it will simply exclaim WTF.

Thus, the method you use is just a helper method of the Clipboard class, which puts data on the clipboard using the standard protocol. That another application can understand, but does not guarantee. The protocol is DataFormats.FileDrop.

The planned replacement will also be very good, you can, of course, put the line on the clipboard. The most basic thing you would ever like to copy / paste. But the application that inserts it only recognizes it as a string. He has no idea that a string should mean something else. The protocol is DataFormats.Text.

The solution is very simple, just write a little private helper method that takes a string. And uses Directory.GetFiles () to create a StringCollection that you put on the clipboard. Simple, mission accomplished, kiss.

+1
source

All Articles