Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



Opening Client-Side Files using Silverlight

Silverlight does not have direct access to the local file system for security reasons. However, you can still prompt the user to select a specific file to open using the OpenFileDialog similarly to how you do in .NET. Here's some basic code that demonstrates opening a user specified file using Silverlight 2 Beta 1.

This basic example opens up a user specified file, and displays its contents within a textbox.

Page.xaml file

<UserControl x:Class="SilverlightFileSystemInfo.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="480">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid HorizontalAlignment="Left">
            <TextBox x:Name="txtFile" Width="400" Height="250" AcceptsReturn="True" />   
        </Grid>
        <Grid HorizontalAlignment="Right" VerticalAlignment="Top">
            <Button x:Name="btnSelectFile" Content="Add File" Width="75" Height="30" Click="btnSelectFile_Click"></Button>
        </Grid>
    </Grid>
</UserControl>

Page.xaml.cs file 

using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace SilverlightOpenFileTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void btnSelectFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            // Show the Open File Dialog
            DialogResult result = fileDialog.ShowDialog();

            // Open the file if OK was clicked in the dialog
            if (result == DialogResult.OK)
            {
                FileDialogFileInfo fi = fileDialog.SelectedFile;
                Stream s = fi.OpenRead();
                StreamReader reader = new StreamReader(s);
                txtFile.Text = reader.ReadToEnd();
            }
        }
    }
}

Now isn't that simple? I wonder if they'll ever add the SaveFileDialog...

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by crpietschmann on Wednesday, March 05, 2008 11:09 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann