简体   繁体   English

FileDrop 格式的奇怪剪贴板行为

[英]Odd Clipboard behaviour with FileDrop format

I'm facing a very strange Clipboard behaviour in my application.我在我的应用程序中遇到了一个非常奇怪的剪贴板行为。 I have a Tcp server that receives some files, saves them to temp locations and puts them in the clipboard.我有一个 Tcp 服务器,它接收一些文件,将它们保存到临时位置并将它们放入剪贴板。 Here's a portion of the code:这是代码的一部分:

filename = bReader.ReadString();
int dim = bReader.ReadInt32();
byte[] buffer = new byte[dim];
buffer = bReader.ReadBytes(dim);
using (FileStream fs = new FileStream(type, FileMode.OpenOrCreate, FileAccess.Write))
{
    fs.Write(buffer, 0, buffer.Length);
    fs.Close();
}                    
String path = Path.GetFullPath(filename);
DataObject data = new DataObject();
data.SetData(DataFormats.FileDrop, true, new String[]{path});
Clipboard.SetDataObject(data, true);

I can receive and save the file correctly, and also put he FileDrop data in the clipboard.我可以正确接收和保存文件,并将 FileDrop 数据放入剪贴板。 The problem is that i can paste the file only when my application gets closed.问题是我只能在我的应用程序关闭时粘贴文件。 That's really weird...这真的很奇怪...

After the application gets closed, i can paste with no problem and the pasted file is completely correct.应用程序关闭后,我可以毫无问题地粘贴,并且粘贴的文件完全正确。

Any suggestions?有什么建议么? Thanks in advance提前致谢

This Could happen, The clipboard is a shared system resource when you call Clipboard.SetDataObject , it calls the user32 API function OpenClipboard , the problem here maybe because your program opens it so other application can't use it while your application is still running.可能发生,当您调用Clipboard.SetDataObject时,剪贴板是共享系统资源,它调用user32 API 函数OpenClipboard ,这里的问题可能是因为您的程序打开了它,因此其他应用程序在您的应用程序仍在运行时无法使用它。 this could also be a problem if you are using a custom meta files on it check this .如果您在其上使用自定义元文件,这也可能是一个问题,请检查 Anyway I run this code "I am using 4.0 if that matters":无论如何,我运行此代码“如果重要,我将使用 4.0”:

DataObject data = new DataObject();
data.SetData(DataFormats.FileDrop, true, new String[] { @"C:\test.txt" });
Clipboard.SetDataObject(data, true);

But I failed to review the issue you described, the windows can see the copy operation while the program is running and after Its closed.但是我没有查看您描述的问题,Windows可以在程序运行时和关闭后看到复制操作。 Are you only access to the Clipboard from that code?您是否只能从该代码访问Clipboard How are you reading the data back "pasting in your form"?您如何读取数据“粘贴在您的表单中”?

Using data.SetData(DataFormats.FileDrop, true, new String[] { @"C:\\test.txt" });使用 data.SetData(DataFormats.FileDrop, true, new String[] { @"C:\\test.txt" }); will not set the clipboard DataObject properly.不会正确设置剪贴板 DataObject。 Tip!小费! If you interrogate clipboard you have to reset it.如果您询问剪贴板,则必须重置它。 From https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setfiledroplist?view=netframework-4.0来自https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setfiledroplist?view=netframework-4.0

DataObject dataObj = new DataObject();
//Create and initializes a new StringCollection.
StringCollection strcolFileList = new StringCollection();
strcolFileList.AddRange(fileList);

try
{
dataObj.SetFileDropList(strcolFileList);
}
catch { }
dataObj.SetData(DataFormats.UnicodeText, Path.GetFullPath(strcolFileList[0]); ); //you can add this for fun
Clipboard.SetDataObject(dataObj);

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

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