Desperately Seeking Love of Sophie

Safe File Open Dialog

with 5 comments

Silverlight applications are sandboxed application that means they can not do anything that requires full trust such as reading the file on disk. This does not mean that Silverlight application can not read file on the disk at all but it means, user needs to allow it read certain file on the disk. This is achieved by Safe FileOpenDialog so that user can navigate the disk and pick the file he/she wants to make it available for the application. Even after this, application never gets the location of the file, it only gets the stream to the content of the file that it can use for things like showing in UI or uploading to webserver etc.

Here is a simple sample that just does that…

 The goal of this application is to read the txt file on the disk and show it in the TextBlock that is part of this application. As First step, create a new silverlight application and then Page.xaml looks like this…

<Canvas
        xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="parentCanvas"
        Loaded="Page_Loaded"
        x:Class="FileOpen.Page;assembly=ClientBin/FileOpen.dll"
        Width="640"
        Height="480"
        Background="White"
        xmlns:uicontrol="clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll">
  <TextBlock x:Name="txtFileContent" Width="557" Height="302" Canvas.Left="40" Canvas.Top="25" Text="TextBlock" TextWrapping="Wrap"/>
  <uicontrol:Button Click="OnClick" Text="Read File"/>
</Canvas>

I needed a UI control that launches the OpenFileDialog. Instead of building my own control, I just used the Button control that is part of Silverlight V1.1 Alpha SDK. This means I added the SilverlightUIControls project to my solution and I added it as a reference for my application.  

Code Behind for this file is pretty straight forward too…

using System;
using System.Windows.Controls;

namespace FileOpen
{
    public partial class Page : Canvas
    {
        public void Page_Loaded(object o, EventArgs e)
        {
            // Required to initialize variables
            InitializeComponent();
        }

        public void OnClick(object o, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.EnableMultipleSelection = false;
            openfile.Filter = "txt files (*.txt)|*.txt";
            openfile.ShowDialog();
            txtFileContent.Text = openfile.SelectedFile.OpenText().ReadToEnd();
        }
    }
}

Written by Vivek

June 1, 2007 at 11:02 pm

Posted in Professional, Software

Tagged with

5 Responses

Subscribe to comments with RSS.

  1. [...] http://vivekdalvi.wordpress.com/2007/06/01/safe-file-open-dialog/ Posted: Saturday, June 02, 2007 12:04 AM by vivekd Filed under: Silverlight [...]

  2. Nice work. I have a query in Safe File Open Dialog. can we have the same feature in Web based Silverlight also – open a file from a disk?

    Gnanavel Madasamy

    October 5, 2007 at 1:31 pm

  3. That feature exists in silverlight. I am not sure if i get what you are asking. Above sample is for silverlight

    vivekdalvi

    October 5, 2007 at 8:04 pm

  4. I wanna ask you one thing…

    1. Is this the same as the way that we used in Windows Application??

    2. Is there any control like Folder Browser control from Window for Silverlight??

    Thanks in advance.

    Michael Sync

    December 22, 2007 at 10:12 am


Leave a Reply