Silverlight-Read External TXT and JPG File Which Is Dragged to Control and Several External Files Dragging

  In this post, I want to talk something about reading txt file or jpg file which is dragged to ListBox control in Silverlight. It shows how to get the file information (including name, path and so on), which is dragged to UIelementA file, by UIelmentA.Drop method and some skills to drag several external files to Silverlight.

How to drag external file to ListBox. At first, we need to set AllowDrop = “True” in ListBox. And then add one Drop method: Drop = “ListBox1_Drop”. Then, Drop method can be triggered when dragging file to ListBox.


<Grid x:Name=“LayoutRoot” Background=“White” Width=“600”>

  <StackPanel HorizontalAlignment=“Left” VerticalAlignment=“Top” Width=“600” Orientation=“Horizontal”>

   <ListBox Name=“listBox1” Background=“AliceBlue” Width=“240” HorizontalAlignment=“Left” VerticalAlignment=“Top” AllowDrop=“True” Height=“400” Drop=“listBox1_Drop”>


   <TextBlock Height=“149” HorizontalAlignment=“Left” Name=“textBlock1” VerticalAlignment=“Top” Width=“323” TextWrapping=“Wrap” />


   <Image Height=“238” Name=“image1” HorizontalAlignment=“Left” VerticalAlignment=“Top” Margin=“240 160 0 0” Stretch=“Fill” Width=“320” Source=“/SLDragFile;component/Images/1_24573_f93ae69954e2e1d.jpg” />


Note: TextBlock displays the TXT file content and Image control displays image content.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Navigation;

using System.IO;

using System.Windows.Markup;

using System.Windows.Media.Imaging;

namespace SLDragFile


    public partial class MainPage : UserControl


        public MainPage()




        private void listBox1_Drop(object sender, DragEventArgs e)


            //Get element related to drag method.

            IDataObject dataObject = e.Data as IDataObject;

            //Return FileInfo array of the dragged external file.

            FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];

            foreach (FileInfo file in files)


                //If File Exists

                if (file.Exists)


                    listBox1.Items.Add(“FileName: “ + file.Name);

                    //If it is txt, read and display.

                    if (file.Extension.ToLower() == “.txt”)


                        StreamReader sreader = file.OpenText();

                        string txtstr = “”;

                        string ReadStr = string.Empty;

                        while ((txtstr = sreader.ReadLine()) != null)


                            ReadStr += txtstr;


                        this.textBlock1.Text = ReadStr;


                    //If is jpg image, read and display.

                    if (file.Extension.ToLower() == “.jpg”)


                        FileStream fs = file.OpenRead();

                        BitmapImage image = new BitmapImage();


                        image1.Source = image;





                    listBox1.Items.Add(“Failed to add file!”);






Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.


One thought on “Silverlight-Read External TXT and JPG File Which Is Dragged to Control and Several External Files Dragging

  1. James Smith says:

    Well done!! Very nice article I really enjoyed it during reading this article. It helped me lot and cleared lot of my doubts in Silverlight file handling. Some good articles also explained very well about read and write file in silverlight which, url of these posts….

    Thanks Everyone!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s