C#拖放文件以形成 [英] C# drag and drop files to form
问题描述
如何以拖放方式将文件加载到表单?
How can I load files to a form in drag and drop way?
哪个活动会上映? (dragdrop,我是对的:) :)
Which event will appearence? ( dragdrop, am i right? :) )
我应该使用哪个组件? (试图使用面板,但没有发生任何事情:()
Which component should I use? (trying to use panel, but nothing happens:( )
在拖动它以形成后,如何确定文件和其他属性的名称?
And how to determine name of file and others properties after drag'n'dropping it to form?
谢谢!
代码
private void panel1_DragEnter(object sender, DragEventsArgs e){
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect = DragDropEffects.Move;
MessageBox.Show(e.Data.GetData(DataFormats.Text).toString());
}
if (e.Data.GetDataPresent(DataFormats.FileDrop)){
}
}
okey,这项工作。
okey, this work.
但是文件怎么样?我如何获得文件的全名,它的.extension?
but how about files? how can I get fullname of file and it's .extension?
而这只是 dragEnter
action
如果我尝试在dragdrop操作中做类似的事情,它不起作用:\
(以释放模式运行)
and this only dragEnter
action.
If i trying make something similar in dragdrop action, it does't work:\
( running in Release-mode )
推荐答案
此代码将l oop遍历并打印拖到窗口中的所有文件的全名(包括扩展名):
This code will loop through and print the full names (including extensions) of all the files dragged into your window:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
Console.WriteLine(filePath);
}
}
这篇关于C#拖放文件以形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!