Desktop Drag and Drop Files

With Silverlight 4 is the new Drop event which is present on all elements derived from UIElement.  With this simple example we will display a Grey area which you can drop a file onto.

<UserControl x:Class="DesktopDragandDrop.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="200" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" AllowDrop="True" Drop="LayoutRoot_Drop" Background="#f0f0f0">
            <TextBlock Text="Drop some files here!" FontSize="24" TextAlignment="Center" VerticalAlignment="Center" />
        </Grid>
        <TextBlock x:Name="output" Grid.Row="1" />
    </Grid>
</UserControl>


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.IO;
using System.Text;

namespace DesktopDragandDrop
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LayoutRoot_Drop(object sender, DragEventArgs e)
        {
            FileInfo[] files = ( FileInfo[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
            StringBuilder content = new StringBuilder();

            foreach (FileInfo file in files)
            {
                content.Append(file.Name);
                content.Append(Environment.NewLine);
            }

            output.Text = content.ToString();
        }
    }
}


As you can see in the LayoutRoot_Drop event handler we are provided with a data object passed in via the event args object.  When dropping files onto your Silverlight application this will be an array of FileInfo objects.

The FileInfo object contains basic file information such as its name and extension and size.  To be able to read the file you will need to be running your application Out of Browser.

Neesha wrote:

I'm unable to drag and drop file.I'm using vs2012.

MarkSilver wrote:

Hi,


I like this drag and drop with files..the only thing is that i can`t get it work on my machine

I've just made a simple silverlight application copy and paste your code, and run it...it runs ok but when i drag and drop a file nothing happens...is this because of a setting in ie9 ? or in vs 2010?


Hope some body can help solving this...i`m new in the silverlight flow..


Best regards,


Mark

Mark wrote:

It looks cool overhere, but when i try to run this code in vs2010 i can`t drop a file in the webform..


I've made a simple silverlight 4 application copy and past the code you have written..and then run it on a windows 7 machine..ie9


Hope you can help with running this code?

Best regards,


Mark

Ron Richins wrote:

Let us compare, drag n' drop.  Asp.NET, how (stateless)?


Silverlight (stateful), client side/server side events and commands.


Personally, I do not care where there are stored, as long as they are available the next time from this visitor, I can improve the response time for that individual.


Cory Seaman,

Just my opinion, but you are placing far too much emphasis on accessing an individuals personal computer.  I love the fact that I can now utilized the cpu, video, and memory of the client personal computer (this includes: local cacheing, multi-core/parralell processing, local storage, and async push of "MY" everything).


The fact that, I do not have access to anything beyond, ("My Documents") should be construde as a positive.  The operating system, has safe guards when launching a program.  If the operating system has not seen this program before, the "Operating System and/or the ant-virus will catch it".


In both cases, your computer will ask you if this is allowed.


From me to you and everyone.


When in doubt, cancel.


If you recognize the requestor, and say okay.  Absolutely, anything that you were not expecting to see, should be deemed a threat. Close immediately, If it will not close, immediately hold your finger on the power button, until every light is off of your computer.  


When you restart your computer, you will get a warning message:


Safe (always safe, however, unable to access the internet).


Normal (ignores the loss of power, check point restart, and restarts as normal).


Ron

parv wrote:

I Liked it

Matt Eland wrote:

Seems to work fine for any random folder on my machine (though attempting to get directory information generates security exceptions).


I've heard this doesn't work as well on Macs. Is this true?

Cory Seaman wrote:

The functionality of the drag and drop feature is still, unfortunately, very limited, it would seem (through the RC version)...even when running out of browser you're still limited to a user's special folders like "My Documents".  I wrote a sample app to .zip files, and the only ones the app has genuine access to are those dragged from the allowed handful of special folders.  I think there's going to be some frustration when the meaning of this starts to become apparent to developers--drag and drop is such a promising feature!

Post your Comments

Neesha wrote:

I'm unable to drag and drop file.I'm using vs2012.

MarkSilver wrote:

Hi,


I like this drag and drop with files..the only thing is that i can`t get it work on my machine

I've just made a simple silverlight application copy and paste your code, and run it...it runs ok but when i drag and drop a file nothing happens...is this because of a setting in ie9 ? or in vs 2010?


Hope some body can help solving this...i`m new in the silverlight flow..


Best regards,


Mark

Mark wrote:

It looks cool overhere, but when i try to run this code in vs2010 i can`t drop a file in the webform..


I've made a simple silverlight 4 application copy and past the code you have written..and then run it on a windows 7 machine..ie9


Hope you can help with running this code?

Best regards,


Mark

Ron Richins wrote:

Let us compare, drag n' drop.  Asp.NET, how (stateless)?


Silverlight (stateful), client side/server side events and commands.


Personally, I do not care where there are stored, as long as they are available the next time from this visitor, I can improve the response time for that individual.


Cory Seaman,

Just my opinion, but you are placing far too much emphasis on accessing an individuals personal computer.  I love the fact that I can now utilized the cpu, video, and memory of the client personal computer (this includes: local cacheing, multi-core/parralell processing, local storage, and async push of "MY" everything).


The fact that, I do not have access to anything beyond, ("My Documents") should be construde as a positive.  The operating system, has safe guards when launching a program.  If the operating system has not seen this program before, the "Operating System and/or the ant-virus will catch it".


In both cases, your computer will ask you if this is allowed.


From me to you and everyone.


When in doubt, cancel.


If you recognize the requestor, and say okay.  Absolutely, anything that you were not expecting to see, should be deemed a threat. Close immediately, If it will not close, immediately hold your finger on the power button, until every light is off of your computer.  


When you restart your computer, you will get a warning message:


Safe (always safe, however, unable to access the internet).


Normal (ignores the loss of power, check point restart, and restarts as normal).


Ron

parv wrote:

I Liked it

Matt Eland wrote:

Seems to work fine for any random folder on my machine (though attempting to get directory information generates security exceptions).


I've heard this doesn't work as well on Macs. Is this true?

Cory Seaman wrote:

The functionality of the drag and drop feature is still, unfortunately, very limited, it would seem (through the RC version)...even when running out of browser you're still limited to a user's special folders like "My Documents".  I wrote a sample app to .zip files, and the only ones the app has genuine access to are those dragged from the allowed handful of special folders.  I think there's going to be some frustration when the meaning of this starts to become apparent to developers--drag and drop is such a promising feature!

Post your Comments

 
 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 2,785

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 2,647

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 2,493

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 2,703

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 2,167

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 2,491

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 2,316

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 2,493

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 3,133

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 2,205

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average