这是一个文件夹的路径,里面有我想要拖放到listBox1中的图片:
C:\ Users \ chocolade1972 \ AppData \ Local \ mws \ My Weather Station \ images
但是,如果我在c:\中拖动一个文件夹,例如c:\ images,那么它将被精确拖动到listBox1中。
当我拖动用户...目录时,我看到这个黑色符号,我无法将其删除。
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
label6.Text = files.Length.ToString();
filesCount = files.Length-1;
beginConvertionToolStripMenuItem.Enabled = true;
for (int i = 0; i < files.Length; i++)
{
FileInfo fi = new FileInfo(files[i]);
if (listBox1.Items.Contains(fi.FullName))
{
}
else
{
string directoryName = fi.DirectoryName;
string[] filesExtension = Directory.GetFiles(directoryName).Select(p => Path.GetExtension(p)).Distinct().OrderBy(p => p).ToArray();
for (int d = 0; d < filesExtension.Length; d++)
{
if (fi.Extension.ToLower() == filesExtension[d])
{
listBox1.Items.Add(fi.FullName);
}
}
}
}
this.Select();
}
}
拖延事件:
private void listBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
bool bfound = false;
for (int i = 0; i < files.Length; i++)
{
FileInfo fi = new FileInfo(files[i]);
string directoryName = fi.DirectoryName;
string[] filesExtension = Directory.GetFiles(directoryName).Select(p => Path.GetExtension(p)).Distinct().OrderBy(p => p).ToArray();
for (int d = 0; d < filesExtension.Length; d++)
{
if (fi.Extension.ToLower() == filesExtension[d])
{
bfound = true;
}
}
}
if (bfound)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
else
e.Effect = DragDropEffects.None;
}