簡體   English   中英

FileDrop 格式的奇怪剪貼板行為

[英]Odd Clipboard behaviour with FileDrop format

我在我的應用程序中遇到了一個非常奇怪的剪貼板行為。 我有一個 Tcp 服務器,它接收一些文件,將它們保存到臨時位置並將它們放入剪貼板。 這是代碼的一部分:

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);

我可以正確接收和保存文件,並將 FileDrop 數據放入剪貼板。 問題是我只能在我的應用程序關閉時粘貼文件。 這真的很奇怪...

應用程序關閉后,我可以毫無問題地粘貼,並且粘貼的文件完全正確。

有什么建議么? 提前致謝

可能發生,當您調用Clipboard.SetDataObject時,剪貼板是共享系統資源,它調用user32 API 函數OpenClipboard ,這里的問題可能是因為您的程序打開了它,因此其他應用程序在您的應用程序仍在運行時無法使用它。 如果您在其上使用自定義元文件,這也可能是一個問題,請檢查 無論如何,我運行此代碼“如果重要,我將使用 4.0”:

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

但是我沒有查看您描述的問題,Windows可以在程序運行時和關閉后看到復制操作。 您是否只能從該代碼訪問Clipboard 您如何讀取數據“粘貼在您的表單中”?

使用 data.SetData(DataFormats.FileDrop, true, new String[] { @"C:\\test.txt" }); 不會正確設置剪貼板 DataObject。 小費! 如果您詢問剪貼板,則必須重置它。 來自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