Reputation: 213
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. 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. 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
Upvotes: 0
Views: 1413
Reputation: 839
Using data.SetData(DataFormats.FileDrop, true, new String[] { @"C:\test.txt" }); will not set the clipboard DataObject properly. Tip! If you interrogate clipboard you have to reset it. From https://learn.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);
Upvotes: 0
Reputation: 16162
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. 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":
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. Are you only access to the Clipboard
from that code? How are you reading the data back "pasting in your form"?
Upvotes: 1