简体   繁体   中英

C# Drag Drop to desktop, and get the directory

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    string[] files = new string[] { @"C:\directory\of\file\to\copy.txt" };
    this.DoDragDrop(new DataObject(DataFormats.FileDrop,files), DragDropEffects.Copy);
} 

This is the code that i used.

Well, it works well, but want to get the directory of the copied file. How can I do this?

使用System.IO的静态Path类,可以提取目录的路径

Path.GetDirectoryName(@"C:\Users\JNK\Desktop\2136D.png");

All you got is the return value of DoDragDrop() to see if the drop actually happened. What the receiving app did with the file is something you cannot find out. Could be anything, including not copying the file at all. A random example is only opening the file in a text editor, the behavior of VS and Notepad.

Note how the example you gave only drags from the desktop, not to the desktop. Use FileBrowserDialog if you need to know where the user wants to copy a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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