C # drag files in and out of winform

Source: Internet
Author: User

To implement a function similar to the resource browser in winform, You need to drag the files listed in winform to other applications or drag the files to winform applications from other applications. There are some articles on the Internet that describe this function, but they are scattered and lack a complete example. For this reason, I have compiled a complete example of drag-in and pull-out implementation files, and wrote this article step by step to explain how to implement similar functions.

Step 1: place a listview in the winform and initialize the following attributes:

 Listview. view = view. Details; <br/> listview. allowdrop = true; <br/>

Step 2: Write a function for displaying the list of directory files

/** // <Summary> <br/> // list files in the folder <br/> /// </Summary> <br/> /// <Param name = "directory"> the directory of the folder </param> <br/> private void listfolder (string directory) <br/>{< br/> labelcurfolder. TEXT = directory; <br/> string [] filelist = system. io. directory. getfiles (directory); <br/> listviewfolder. items. clear (); <br/> listviewfolder. columns. clear (); <br/> listviewfolder. columns. add ("name", 300); <br/> listviewfolder. columns. create ("size", 100); <br/> listviewfolder. columns. add ("time", 200); <br/> foreach (string filename in filelist) <br/> {<br/> // show file name <br/> listviewitem itemname = new listviewitem (system. io. path. getfilename (filename); <br/> itemname. tag = filename; <br/> // show file icon <br/> iconimageprovider = new iconimageprovider (listviewfolder. smallimagelist, <br/> listviewfolder. largeimagelist); <br/> itemname. imageindex = iconimageprovider. geticonimageindex (filename); <br/> // show file size <br/> system. io. fileinfo = new system. io. fileinfo (filename); <br/> long size = fileinfo. length; <br/> string strsize; <br/> If (size <1024) <br/>{< br/> strsize = size. tostring (); <br/>}< br/> else if (size <1024*1024) <br/>{< br/> strsize = string. format ("{0 :###. ##} kb ", (float) size/1024); <br/>}< br/> else if (size <1024*1024*1024) <br/>{< br/> strsize = string. format ("{0 :###. ##} MB ", (float) size/(1024*1024 )); <br/>}< br/> else <br/>{< br/> strsize = string. format ("{0 :###. ##} GB ", (float) size/(1024*1024*1024); <br/>}< br/> listviewitem. listviewsubitem subitem = new listviewitem. listviewsubitem (); <br/> subitem. TEXT = strsize; <br/> subitem. tag = size; <br/> itemname. subitems. add (subitem); <br/> // show file time <br/> subitem = new listviewitem. listviewsubitem (); <br/> datetime filetime = system. io. file. getlastwritetime (filename); <br/> subitem. TEXT = (string) filetime. tolocaltime (). tostring ("yyyy-mm-dd hh: mm: SS"); <br/> subitem. tag = filetime; <br/> itemname. subitems. add (subitem); <br/> listviewfolder. items. add (itemname); <br/>}< br/>

Step 3: Add a dragenter event to the listview

The dragenter event determines the type of the currently dragged object when the files dragged by other applications enter. If it is a file type, set the drag response type to copy.

Private void listviewfolder_dragenter (Object sender, drageventargs e) <br/>{< br/> If (E. data. getdatapresent (dataformats. filedrop) <br/>{< br/> E. effect = dragdropeffects. copy; <br/>}< br/> else <br/>{< br/> E. effect = dragdropeffects. none; <br/>}< br/>

Step 4: Add a dragdrop event to the listview

The dragdrop event completes copying the files dragged by other applications to the current directory of the winform application.

Private void listviewfolder_dragdrop (Object sender, drageventargs e) <br/>{< br/> try <br/>{< br/> string [] files = E. data. getdata (dataformats. filedrop, false) as string []; <br/> // copy file from external application <br/> foreach (string srcfile in files) <br/>{< br/> string destfile = labelcurfolder. text + "" + system. io. path. getfilename (srcfile); <br/> If (system. io. file. exists (destfile) <br />{< Br/> If (MessageBox. show (string. format (<br/> "this folder already contains a file named {0}, wocould you like to replace the existing file", <br/> system. io. path. getfilename (srcfile), <br/> "Confirm file Replace", messageboxbuttons. yesno, messageboxicon. none )! = <Br/> dialogresult. yes) <br/>{< br/> continue; <br/>}< br/> system. io. file. copy (srcfile, destfile, true); <br/>}< br/> // list current folder <br/> listfolder (); <br/>}< br/> catch (exception E1) <br/>{< br/> MessageBox. show (e1.message, "error", messageboxbuttons. OK, messageboxicon. error); <br/>}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.