Drag file onto form to open file


/ Published in: C#
Save to your folder(s)

Example written 01/13/2011

The following code shows how to implement functionality in your Windows Forms application, such that you'll be able to open a file that is dragged and dropped onto your program's form. This behavior is often desirable for programs that manipulate files in some way (renamers, encrypters, etc.) It's also nice for the uer to be able to have more than one way to open a file. This example implements opening via drag-dropping, and via a customary File->Open menu.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. namespace BryanWinForms1 {
  7. public partial class Form1 : Form {
  8.  
  9. private delegate void DelegateOpenFile(String s); //define delegate type
  10. DelegateOpenFile _openFileDelegate; //declares a delegate instance
  11.  
  12. public Form1() {
  13. InitializeComponent();
  14. this.AllowDrop = true; //must set this to true, for dragDrop to work
  15. //delegate needed so Form1_DragDrop() can asynchronously
  16. //invoke our program's OpenFile() method
  17. _openFileDelegate = new DelegateOpenFile(this.OpenFile); //instantiates delegate
  18. }
  19.  
  20. private void openToolStripMenuItem_Click(object sender, EventArgs e) {
  21. OpenFileDialog openDlg = new OpenFileDialog();
  22. openDlg.Filter = "Any File (*.*)|*.*";
  23. openDlg.FileName = "";
  24. openDlg.CheckFileExists = true;
  25. openDlg.CheckPathExists = true;
  26.  
  27. if (openDlg.ShowDialog() != DialogResult.OK)
  28. return;
  29.  
  30. OpenFile(openDlg.FileName);
  31. }
  32.  
  33. private void OpenFile(string sFile) {
  34. //insert appropriate file-opening code here...
  35. MessageBox.Show("\"" + sFile + "\" will be opened.");
  36. }
  37.  
  38. private void Form1_DragEnter(object sender, DragEventArgs e) {
  39. //we're only interested if a FILE was dropped on the form
  40. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  41. e.Effect = DragDropEffects.Copy;
  42. else
  43. e.Effect = DragDropEffects.None;
  44. }
  45.  
  46. private void Form1_DragDrop(object sender, DragEventArgs e) {
  47. //good idea to use try-catch block, in case something goes wrong
  48. try {
  49. Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
  50. if (a != null) {
  51. // Extract string from first array element
  52. // (ignore all files except first if number of files are dropped).
  53. string s = a.GetValue(0).ToString();
  54. // Call OpenFile asynchronously.
  55. // Explorer instance from which file is dropped is not responding
  56. // the entire time that the DragDrop handler is active, so we need to return
  57. // immidiately (especially if OpenFile shows MessageBox).
  58. this.BeginInvoke(_openFileDelegate, new Object[] { s });
  59. this.Activate(); // in the case Explorer overlaps this form
  60. }
  61. }
  62. catch (Exception ex) {
  63. Trace.WriteLine("Error in DragDrop function: " + ex.Message);
  64. // don't show MessageBox here - Explorer is waiting !
  65. }
  66. }
  67.  
  68. //next right-brace ends Form1 class
  69. }
  70. //next right-brace ends namespace
  71. }

URL: http://kyrathaba.dcmembers.com/errata/openFileDroppedOntoForm.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.