C# 如何将文件拖放到应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/68598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I drag and drop files into an application?
提问by
回答by Judah Gabriel Himango
In Windows Forms, set the control's AllowDrop property, then listen for DragEnter event and DragDrop event.
在 Windows 窗体中,设置控件的 AllowDrop 属性,然后监听 DragEnter 事件和 DragDrop 事件。
When the DragEnter
event fires, set the argument's AllowedEffect
to something other than none (e.g. e.Effect = DragDropEffects.Move
).
当DragEnter
事件触发时,将参数设置为AllowedEffect
none 以外的值(例如e.Effect = DragDropEffects.Move
)。
When the DragDrop
event fires, you'll get a list of strings. Each string is the full path to the file being dropped.
当DragDrop
事件触发时,您将获得一个字符串列表。每个字符串都是被删除文件的完整路径。
回答by Phil Wright
You need to be aware of a gotcha. Any class that you pass around as the DataObjectin the drag/drop operation has to be Serializable. So if you try and pass an object, and it is not working, ensure it can be serialized as that is almost certainly the problem. This has caught me out a couple of times!
你需要知道一个陷阱。在拖放操作中作为DataObject传递的任何类都必须是可序列化的。因此,如果您尝试传递一个对象,但它不起作用,请确保它可以被序列化,因为这几乎肯定是问题所在。这已经让我失望了几次!
回答by Craig Eddy
Another common gotcha is thinking you can ignore the Form DragOver (or DragEnter) events. I typically use the Form's DragOver event to set the AllowedEffect, and then a specific control's DragDrop event to handle the dropped data.
另一个常见问题是认为您可以忽略 Form DragOver(或 DragEnter)事件。我通常使用 Form 的 DragOver 事件来设置 AllowedEffect,然后使用特定控件的 DragDrop 事件来处理拖放的数据。
回答by Hans Passant
Some sample code:
一些示例代码:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
void Form1_DragDrop(object sender, DragEventArgs e) {
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files) Console.WriteLine(file);
}
}
回答by Guge
Yet another gotcha:
另一个问题:
The framework code that calls the Drag-events swallow all exceptions. You might think your event code is running smoothly, while it is gushing exceptions all over the place. You can't see them because the framework steals them.
调用拖动事件的框架代码会吞下所有异常。您可能认为您的事件代码运行顺利,但它却在到处涌现异常。您看不到它们,因为框架会窃取它们。
That's why I always put a try/catch in these event handlers, just so I know if they throw any exceptions. I usually put a Debugger.Break(); in the catch part.
这就是为什么我总是在这些事件处理程序中放置一个 try/catch,这样我就知道它们是否会抛出任何异常。我通常会放一个 Debugger.Break(); 在捕获部分。
Before release, after testing, if everything seems to behave, I remove or replace these with real exception handling.
在发布之前,在测试之后,如果一切似乎都正常,我会删除或用真正的异常处理替换它们。
回答by Wayne Uroda
Be aware of windows vista/windows 7 security rights - if you are running Visual Studio as administrator, you will not be able to drag files from a non-administrator explorer window into your program when you run it from within visual studio. The drag related events will not even fire! I hope this helps somebody else out there not waste hours of their life...
请注意 windows vista/windows 7 安全权限 - 如果您以管理员身份运行 Visual Studio,当您从 Visual Studio 中运行它时,您将无法将文件从非管理员资源管理器窗口拖动到您的程序中。拖动相关事件甚至不会触发!我希望这可以帮助其他人不要浪费他们的生活时间......
回答by CAD bloke
Here is something I used to drop files and/or folders full of files. In my case I was filtering for *.dwg
files only and chose to include all subfolders.
这是我用来删除文件和/或充满文件的文件夹的东西。就我而言,我仅过滤*.dwg
文件并选择包括所有子文件夹。
fileList
is an IEnumerable
or similar In my case was bound to a WPF control...
fileList
是一个IEnumerable
或类似的在我的情况下绑定到一个 WPF 控件......
var fileList = (IList)FileList.ItemsSource;
See https://stackoverflow.com/a/19954958/492for details of that trick.
有关该技巧的详细信息,请参阅https://stackoverflow.com/a/19954958/492。
The drop Handler ...
掉落处理程序...
private void FileList_OnDrop(object sender, DragEventArgs e)
{
var dropped = ((string[])e.Data.GetData(DataFormats.FileDrop));
var files = dropped.ToList();
if (!files.Any())
return;
foreach (string drop in dropped)
if (Directory.Exists(drop))
files.AddRange(Directory.GetFiles(drop, "*.dwg", SearchOption.AllDirectories));
foreach (string file in files)
{
if (!fileList.Contains(file) && file.ToLower().EndsWith(".dwg"))
fileList.Add(file);
}
}
回答by Roland
回答by Ernest Rutherford
You can implement Drag&Drop in WinForms and WPF.
您可以在 WinForms 和 WPF 中实现拖放。
- WinForm (Drag from app window)
- WinForm(从应用程序窗口拖动)
You should add mousemove event:
您应该添加 mousemove 事件:
private void YourElementControl_MouseMove(object sender, MouseEventArgs e)
{
...
if (e.Button == MouseButtons.Left)
{
DoDragDrop(new DataObject(DataFormats.FileDrop, new string[] { PathToFirstFile,PathToTheNextOne }), DragDropEffects.Move);
}
...
}
- WinForm (Drag to app window)
- WinForm(拖动到应用程序窗口)
You should add DragDrop event:
您应该添加 DragDrop 事件:
private void YourElementControl_DragDrop(object sender, DragEventArgs e)
private void YourElementControl_DragDrop(object sender, DragEventArgs e)
{
...
foreach (string path in (string[])e.Data.GetData(DataFormats.FileDrop))
{
File.Copy(path, DirPath + Path.GetFileName(path));
}
...
}