C # WinForm Next step to drag and drop files

Source: Internet
Author: User
Tags file size filetime tostring

Implementing a resource-like browser functionality in WinForm requires implementation to drag the files listed in WinForm out into other applications or drag files from other applications into the WinForm application. There are some articles on the web that describe this feature, but they are all quite fragmented and lack a complete example. For this I have written a more complete example of drag-and-drop implementation files, and write this step-by-step explanation if similar functionality is implemented.

Step 1 places a ListView into the WinForm form and initializes the following properties:

listView.View = View.Details;
listView.AllowDrop = true;

Step 2 Compose a list of the functions shown in a directory file listing

/**////<summary>
List files in the folder
</summary>
<param name= "directory" >the directory of the Folder</param>
private void Listfolder (String directory)
{
Labelcurfolder.text = Directory;
string[] filelist = System.IO.Directory.GetFiles (Directory);
ListViewFolder.Items.Clear ();
ListViewFolder.Columns.Clear ();
LISTVIEWFOLDER.COLUMNS.ADD ("Name", 300);
LISTVIEWFOLDER.COLUMNS.ADD ("Size", 100);
LISTVIEWFOLDER.COLUMNS.ADD ("Time", 200);
foreach (String fileName in FileList)
{
Show file name
ListViewItem itemname = new ListViewItem (System.IO.Path.GetFileName (fileName));
Itemname.tag = FileName;
Show file icon
Iconimageprovider Iconimageprovider = new Iconimageprovider (listviewfolder.smallimagelist,
Listviewfolder.largeimagelist);
Itemname.imageindex = Iconimageprovider.geticonimageindex (fileName);
Show file Size
System.IO.FileInfo FileInfo = new System.IO.FileInfo (fileName);
Long size = Fileinfo.length;
String strsize;
if (Size < 1024)
{
Strsize = size. ToString ();
}
else if (Size < 1024 * 1024)
{
Strsize = String.Format ("{0:###.##}kb", (float) size/1024);
}
else if (Size < 1024 * 1024 * 1024)
{
Strsize = String.Format ("{0:###.##}MB", (float) size/(1024 * 1024));
}
Else
{
Strsize = String.Format ("{0:###.##}GB", (float) size/(1024 * 1024 * 1024));
}
Listviewitem.listviewsubitem subitem = new Listviewitem.listviewsubitem ();
Subitem.text = strsize;
Subitem.tag = size;
ITEMNAME.SUBITEMS.ADD (subitem);
Show file time
subitem = new Listviewitem.listviewsubitem ();
DateTime filetime = System.IO.File.GetLastWriteTime (fileName);
Subitem.text = (string) filetime.tolocaltime (). ToString ("Yyyy-mm-dd HH:mm:ss");;
Subitem.tag = FILETIME;
ITEMNAME.SUBITEMS.ADD (subitem);
LISTVIEWFOLDER.ITEMS.ADD (ItemName);
}
}

In the code above, there is a section of the code that shows the icon because it has nothing to do with the drag, I do not post it, interested can download the full code to see.

Related Article

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.