简体   繁体   English

Clipboard.SetFileDropList不能正常工作

[英]Clipboard.SetFileDropList doesn't work fine

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. 我尝试了SetData对象,但它是相同的。

EDIT 2: 编辑2:

The filedroplist seems to be into the clipboard but the paste is disabled in the system. 文件下拉列表似乎在剪贴板中,但是在系统中禁用了粘贴。

EDIT 3: 编辑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. 唯一的方法是调用win32 Dll。

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. Clipboard类只能在设置为单线程单元(STA)模式的线程中使用。 The options to do this are 为此的选项是

  1. Mark Main method with the STAThreadAttribute attribute. 用STAThreadAttribute属性标记Main方法。

OR 要么

  1. Create a STA thread from your application and use Clipboard 从您的应用程序创建STA线程并使用剪贴板

Example code for option #2 选项#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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM