Why Clipboard.GetFileDropList() returns an empty collection on Remote Desktop?
Asked Answered
T

1

6

I have a program on which I need to be able to copy and paste from a remote computer, to my local computer.

My problem is, when I use

Clipboard.GetDropList();

it returns a collection with 0 elements, no matter how many elements I tried to copy.

I tried it with :

if (Clipboard.ContainsFileDropList())
{
    foreach (string item in Clipboard.GetFileDropList())
    {
        File.Copy(item, path + '\\' + Path.GetFileName(item));
    }
}

I also tried (BoltBait's answer):

System.Collections.Specialized.StringCollection idat = null;
Exception threadEx = null;
Thread staThread = new Thread(
    delegate ()
    {
        try
        {
            idat = Clipboard.GetFileDropList();
        }
        catch (Exception ex)
        {
            threadEx = ex;
        }
    });
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();

Both versions return an empty collection.

Where could the problem come from? Knowing that:

  • Copy/Pasting from Remote to local (via windows) works
  • Copy/Pasting from Local to Local (via code) works
  • Clipboard sharing is activated
  • OS (Local) : Windows 10
  • OS (Remote) : Windows Server 2008 R2
Thurifer answered 13/9, 2017 at 9:38 Comment(0)
R
3

The reason why you get an empty collection is the fact that the clipboard doesn't contain any data in the DataFormats.FileDrop format.

Instead, on copying some files on a remote machine via Remote Desktop (while the clipboard sharing is enabled), the file contents will be placed in the clipboard directly. The clipboard will contain the data in following formats:

So theoretically you could try to iterate through the FILEGROUPDESCRIPTOR objects and store each file reading its CFSTR_FILECONTENTS from the clipboard.

But I found a bug report describing that this only works for the first file in Windows Forms. So you will have to implement it using P/Invoke by calling the native methods.

Rameriz answered 13/9, 2017 at 10:30 Comment(2)
When I try to access FileContents I get an exception (CLIPBRD_E_BAD_DATA HRESULT : 0x800401D3) (I'm using GetData("FileContents") ). Is there another method I should use ?Thurifer
Unfortunately, this is not well supported by the Clipboard implementation in .NET. You have to switch to unmanaged approach - either via P/Invoke or by creating your own unmanaged module. For details, see this topic.Rameriz

© 2022 - 2024 — McMap. All rights reserved.