简体   繁体   English

C#Drag Drop to desktop,并获取目录

[英]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. 你得到的只是DoDragDrop()的返回值,看看实际上是否发生了丢弃。 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. 随机示例仅在文本编辑器中打开文件,VS和记事本的行为。

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. 如果您需要知道用户想要复制文件的位置,请使用FileBrowserDialog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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