如何托拽文件到form的某个控件上,如何得到这个文件的信息?包括文件名,路径,属性等信息,类似很多应用程序可以支持推拽文件或者快捷方式?哪位高手能给指点一下,最好有例子,谢了.
解决方案 »
- 一道简约的C#题。。。
- windows mobile中如何引用Web Service?
- ReDim BaseData(30, 3) As String是什么意思?(抢分贴)
- 如何更改label边框的颜色?
- OleDb操作Excel文档的问题(如何获取指定字段上的某个值所在的行号)
- 最后20分了,请问大家有没有做过windows服务的东西!!!
- 求asp.net C#如何检测服务器的CPU个数
- 如何屏蔽axAcroPDF的右键菜单或是控制其菜单项
- C# C/S应用程序 反序列化时出错 错误源是mscorlib
- !!!兄弟们来帮忙啦!一个图文混排文章数据库管理系统软件策划中!
- 为什么看不到图片!?请各位帮帮忙!急,马上结贴!
- 我用WebClient类中的DownloadFile为什么不能从Win2003上下载数据!在2000上没问题!UP也有分
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
} private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
//其中label1.Text显示的就是拖进文件的文件名;
label1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}Form1换成要拖动到的控件
{
// for this program, we allow a file to be dropped from Explorer
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{ e.Effect = DragDropEffects.Copy;}
// or this tells us if it is an Outlook attachment drop
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{ e.Effect = DragDropEffects.Copy;}
// or none of the above
else
{ e.Effect = DragDropEffects.None;}
} private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string [] fileNames = null; try
{
if ( e.Data.GetDataPresent(DataFormats.FileDrop,false) == true)
{
fileNames = (string []) e.Data.GetData(DataFormats.FileDrop);
// handle each file passed as needed
foreach( string fileName in fileNames)
{
// do what you are going to do with each filename
}
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
//
// the first step here is to get the filename of the attachment and
// build a full-path name so we can store it in the temporary folder
// // set up to obtain the FileGroupDescriptor and extract the file name
Stream theStream = (Stream) e.Data.GetData("FileGroupDescriptor");
byte [] fileGroupDescriptor = new byte[512];
theStream.Read(fileGroupDescriptor,0,512);
// used to build the filename from the FileGroupDescriptor block
StringBuilder fileName = new StringBuilder("");
// this trick gets the filename of the passed attached file
for(int i=76; fileGroupDescriptor[i]!=0; i++)
{ fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));}
theStream.Close();
string path = Path.GetTempPath(); // put the zip file into the temp directory
string theFile = path+fileName.ToString(); // create the full-path name //
// Second step: we have the file name. Now we need to get the actual raw
// data for the attached file and copy it to disk so we work on it.
// // get the actual raw file into memory
MemoryStream ms = (MemoryStream) e.Data.GetData("FileContents",true);
// allocate enough bytes to hold the raw data
byte [] fileBytes = new byte[ms.Length];
// set starting position at first byte and read in the raw data
ms.Position = 0;
ms.Read(fileBytes,0,(int)ms.Length);
// create a file and save the raw zip file to it
FileStream fs = new FileStream(theFile,FileMode.Create);
fs.Write(fileBytes,0,(int)fileBytes.Length); fs.Close(); // close the file FileInfo tempFile = new FileInfo(theFile); // always good to make sure we actually created the file
if ( tempFile.Exists == true)
{
// for now, just delete what we created
tempFile.Delete();
}
else
{ Trace.WriteLine("File was not created!");}
} }
catch (Exception ex)
{
Trace.WriteLine("Error in DragDrop function: " + ex.Message); // don't use MessageBox here - Outlook or Explorer is waiting !
}
}