C # Drag and Drop a file using a form

Source: Internet
Author: User

1. Set the AllowDrag attribute of your form or space to allow. 2. register the DragEnter event. 3. Obtain the file path. data. getFormats () method to obtain all data formats 4. Call e. the GetData () method obtains the file path based on the specified data format. 5. With the file path, I believe all problems are solved. The following is an example: [csharp] private void textbox#dragenter (object sender, DragEventArgs e) {// obtain the data format File. writeAllLines ("C: \ 1. text ", e. data. getFormats (); // get the file name string [] filename = (string []) e. data. getData ("FileNameW", true); textBox1.Text = filename [0]; string entesion = Path. GetFileName (textBox1.Text); if (! File. exists (textBox1.Text. trim () {return;} // read the File stream using (FileStream fsOpen = File. openRead (textBox1.Text. trim () {using (FileStream fs = new FileStream ("C :\\" + entesion, FileMode. create, FileAccess. write) {byte [] buffer = new byte [1024*1024]; while (true) {int result = fsOpen. read (buffer, 0, buffer. length); if (result <= 0) {break;} else {fs. write (buffer, 0, result) ;}} fs. flush ();} Running result: In win7, The DragDrop event may not be triggered due to unbalanced permissions, so I only use the DragEnter event in it. You can try the DragDrop event, the user experience may be better!

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.