tuman
tuman

Reputation: 101

Drag file from WPF App

I find a lot of samples where describe how to drag a file from a WPF app. I just need to export .txt or .csv file from my app when the user try drag listbox item.

var filestream = File.Create(@"C:\Users\myuser\Documents\test.txt");
var barray = Encoding.Unicode.GetBytes("Some text");
filestream.Write(barray, 0, barray.Length);
//filestream.Close();
DataObject data = new DataObject(DataFormats.FileDrop, filestream);
DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);
//filestream.Close();

But if I release on Desktop there isn't any file copied. What is wrong?

Upvotes: 1

Views: 549

Answers (1)

AQuirky
AQuirky

Reputation: 5236

The reason file drop is not working is that you are not providing a list of files paths which is the requirement for this clipboard format. To fix this...

var filename = @"C:\Users\myuser\Documents\test.txt"
var filenames = new string[] { filename };
var filestream = File.Create(filename);
var barray = Encoding.Unicode.GetBytes("Some text");
filestream.Write(barray, 0, barray.Length);
filestream.Close();

DataObject data = new DataObject(DataFormats.FileDrop, filenames);
DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);

Upvotes: 3

Related Questions