1

I have some lines of code in a method that change my clipboard content:

System.Collections.Specialized.StringCollection stC = new System.Collections.Specialized.StringCollection();
stC.AddRange(System.IO.Directory.GetDirectories(tempPath));
stC.AddRange(System.IO.Directory.GetFiles(tempPath));

Clipboard.Clear();
Clipboard.SetFileDropList(stC);

When I go in debug mode and put a breakpoint into my method all works fine and the clipboard is updated, but the content in the clipboard is not available when my method ends (my folder is not destroyed obviously).

Some ideas?

EDIT:

If I break the execution with a message box before exit it works, otherwise it does not. I tried with SetData object, but it is the same.

EDIT 2:

The filedroplist seems to be into the clipboard but the paste is disabled in the system.

EDIT 3:

I think I've found the problem: the only reason can be because the app takes ownership of the clipboard and does not release it until is closed, so it does not allow external usage of actual content. The only way is to invoke win32 Dll.

b4hand
  • 9,550
  • 4
  • 44
  • 49
Alexander.It
  • 187
  • 1
  • 1
  • 16

1 Answers1

0

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. The options to do this are

  1. Mark Main method with the STAThreadAttribute attribute.

OR

  1. Create a STA thread from your application and use Clipboard

Example code for option #2

System.Collections.Specialized.StringCollection stC = new System.Collections.Specialized.StringCollection();
stC.AddRange(System.IO.Directory.GetDirectories(tempPath));
stC.AddRange(System.IO.Directory.GetFiles(tempPath));

//Clipboard.Clear(); //No need to Call this.

//>Call from an STA thread
Thread t = new Thread(() => { 
                              Clipboard.SetFileDropList(stC); 
                            });
t.SetApartmentState(ApartmentState.STA);
t.Start();
Sunil Purushothaman
  • 8,435
  • 1
  • 22
  • 20