Drag and Drop Text Files from Windows Explorer to your Windows Form Application
Posted by: Suprotim Agarwal ,
on 8/24/2008,
in
Category WinForms & WinRT
Abstract: This is a short article describing how you can drag and drop files from your Desktop/Windows Explorer to your Windows Form application. In this article, we will drag text files from the desktop and drop it on our WinForm application, to display the contents of the text files.
Drag and Drop Text Files from Windows Explorer to your Windows Form Application
This is a short article describing how you can drag and drop files from your Desktop/Windows Explorer to your Windows Form application. In this article, we will drag text files from the desktop and drop it on our WinForm application, to display the contents of the text files. Let us see the steps required to do so:
Step 1: Open Visual Studio > File > New > Project. In the Project Types pane, choose the language of your choice (Visual C# or Visual Basic). In the Templates pane, choose Windows Application. Choose a name and location for the project and click OK.
Step 2: To set up the functionality for the drag drop operation, let us take a quick overview of the steps involved to do so. Let us assume that this WinForm application is ready and is running. The user will select one or more text files on the desktop or Windows Explorer. The user will then drag the selected files kept on the desktop, on to the running application form. The contents of all the text files will then be displayed in the application. To complete the operation, the following events need to be handled.
Form DragEnter Event – this event will be handled when the file is dragged into the Form’s bounds.
Form DragDrop Event - Occurs when a drag-and-drop operation is completed.
Step 3: In the form load event, register the events for the drag drop operation as shown below. Notice that the ‘AllowDrop’ property has to be set to true to be able to drop items on the form:
C#
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragEnter += Form1_DragEnter;
this.DragDrop += Form1_DragDrop;
}
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.AllowDrop = True
Me.DragEnter += Form1_DragEnter
Me.DragDrop += Form1_DragDrop
End Sub
Step 4: Moving ahead, we will now add code to the respective events discussed above:
C#
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths)
{
// Code to read the contents of the text file
if (File.Exists(fileLoc))
{
using (TextReader tr = new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadToEnd());
}
}
}
}
}
VB.NET
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim filePaths As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each fileLoc As String In filePaths
' Code to read the contents of the text file
If File.Exists(fileLoc) Then
Using tr As TextReader = New StreamReader(fileLoc)
MessageBox.Show(tr.ReadToEnd())
End Using
End If
Next fileLoc
End If
End Sub
In the Form1_DragEnter we use the DataFormats.FileDrop field to interact with shell file drags, when a drag-and-drop operation occurs. This method is called before GetData().
Finally in the Form1_DragDrop, we will extract the file path(s) using the GetDate() method in a string array. So if two text files are dragged from the desktop to the application, the string array will contain two file paths. The last step is to loop through the file paths and use the StreamReader to read the contents of the text file.
That’s it. Run the sample and you will be able to perform drag drop operations from the desktop/Windows Explorer to the running application form. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn