在更改剪贴板内容的方法中,我有几行代码:
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);当我进入调试模式并将一个断点放入我的方法中时,一切正常,剪贴板被更新,但是当我的方法结束时,剪贴板中的内容是不可用的(我的文件夹没有明显破坏)。
一些想法?
编辑:
如果我在退出前用消息框中断执行,它就无效,否则它就无效。我尝试过使用SetData对象,但它是一样的。
编辑2:
文件列表似乎已进入剪贴板,但粘贴在系统中是禁用的。
编辑3:
我想我已经发现了问题:唯一的原因可能是因为应用程序拥有剪贴板,直到关闭之后才会发布,所以它不允许外部使用实际内容。唯一的方法是调用win32 Dll。
发布于 2015-09-24 09:00:22
剪贴板类只能在设置为单线程单元(STA)模式的线程中使用。这样做的选择如下
或
选项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();https://stackoverflow.com/questions/26950866
复制相似问题