Page 1 of 1

Drag and drop a file on another file Opening a file by dragging and dropping a file on another file

#1 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Posted 29 December 2009 - 12:52 PM

I decided to write this tutorial since I find it really useful and I'm going to show you how it is possible to open some file and show it's content by just dragging and dropping a file on your application's executable file in Windows Explorer. In my example I made a simple application that can accept any image file and than shows the image in a picture box that I added on the form, but you can use any other file type and do whatever you need to do with this file.


For a start, just create a Windows application project. Then add a picture box control on your form and name it pbMyPictureBox or whatever you like. I changed tha back color of my picturebox so it is seen where it is positioned.

Attached Image

After that open the code view of your form and we are going to modify and add few lines of code. I modified the default Form1 constructor so that it accepts an array of strings. Then I created a private method named InitCustomComponents that also accepts an array of string and is called just after the Form1 is initialized.

In my InitCustomComponents method I first check if passed arguments are null and if there is any items in array at all. Then I read the first item in array which is actually a path of an image file that is going to be dropped on our application's executable.

Of course it is possible that user drags and drops some other file type (not an image), that is why a enclosed the following code inside try catch block, to catch this kind of scenario. If file is an image, just create a new Bitmap from that file, and show it in a picture box, otherwise an exception will be thrown and user will be informed that there was an invalid parameter, but you can write any other message to be displayed to user.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DroppingFileApp
{
	public partial class Form1 : Form
	{
		//Modified Form1 constructor to accept arguments
		public Form1(string[] args)
		{
			InitializeComponent();

			//Call the method that reads arguments 
			//and shows the image in picturebox
			InitCustomComponents(args);
		}

		private void InitCustomComponents(string[] args)
		{
			//Verify if there are any arguments at all
			if (args != null && args.Length == 1)
			{
				//read the first argument in args string array
				string file = args[0];

				//Wrap the code in try catch block since it is
				//possible that argument is not a file or not
				//a picture
				try
				{
					Bitmap image = new Bitmap(file);
					pbMyPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
					pbMyPictureBox.Image = image;
				}
				catch (Exception ex)
				{
					//Inform a user that there was an error
					MessageBox.Show(ex.Message);
				}
			}
		}
	}
}



Now that our Form1 class is complete, we can make just a few more changes to our Program.cs class. This class is actually an entry point for our application and this class is the one that creates our Form1 instance and initializes it.

As you can see I only modified two lines of code in this file. First of all, a modified Main constructor to accept an array of string (args) and then when Application.Run method is called I passed the args variable to Form1 constructor.

using System;
using System.Windows.Forms;

namespace DroppingFileApp
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args) //Modified Main constructor to accept arguments
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			//Pass the args argument in Form1 constructor
			Application.Run(new Form1(args));
		}
	}
}



Now, compile your application, go to your Windows Explorer, find the application's executable file and then drag some image file and drop it on our executable and our application should open and show the picture.

Attached Image

Attached Image


But there is more :-) You can even open an image with our application using Command Prompt (like most windows programs provide this feature):
To do this, open the Command Prompt, navigate to directory where your application's executable exists and type the following (use the name of your application instead):

C:\>DroppingFileApp.exe "C:\Ales\Temp\firstStep.jpg"

Attached Image


That's it. I hope you like this tutorial and that you learned something new.

Is This A Good Question/Topic? 0
  • +

Replies To: Drag and drop a file on another file

#2 dana sorani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-March 14

Posted 06 March 2014 - 10:39 AM

Great Tutorial Well Done (y)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1