Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and writing local files in Silverlight/C#
01-10-2013, 06:01 AM
Post: #1
Reading and writing local files in Silverlight/C#
Lets see how to read and write a text file in Silverlight.

First make a Silverlight project. Add one Button for Open, one for Save and a TextBox to show and modify File Contents. Remember, you need to have a FileDialog to interact with user files in Silverlight for the sake of security. We will be using OpenFileDialog to open file and SaveFileDialog to save any changes. Your XAML will look like this:

<UserControl x:Class="LocalFileReadWrite.MainPage"
    d:DesignHeight="800" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Open" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="OpenFileButton" VerticalAlignment="Top" Width="75" Click="OnOpenFileButtonClick" />
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="93,12,0,0" Name="SaveFileButton" VerticalAlignment="Top" Width="75" Click="OnSaveFileButtonClick" />
        <TextBox Margin="12,41,12,12" Name="FileContentsTextBox" TextWrapping="Wrap" AcceptsReturn="True" />

And in your C# code file, paste the following code:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Text;
using System.IO;

namespace LocalFileReadWrite
    public partial class MainPage : UserControl
        private const String FileTypeFilter = "Text files (*.txt)|*.txt";

        public MainPage()

        private void OnOpenFileButtonClick(object sender, RoutedEventArgs e)
            OpenFileDialog _OpenFileDialog = new OpenFileDialog();
            _OpenFileDialog.Filter = FileTypeFilter;

            if (_OpenFileDialog.ShowDialog() == true)
                System.IO.FileStream fileStream = _OpenFileDialog.File.OpenRead();
                byte[] FileContentsBytes = new byte[fileStream.Length];
                fileStream.Read(FileContentsBytes, 0, FileContentsBytes.Length);
                FileContentsTextBox.Text = UTF8Encoding.UTF8.GetString(FileContentsBytes, 0, FileContentsBytes.Length);

        private void OnSaveFileButtonClick(object sender, RoutedEventArgs e)
            SaveFileDialog _SaveFileDialog = new SaveFileDialog();
            _SaveFileDialog.Filter = FileTypeFilter;

            if (_SaveFileDialog.ShowDialog() == true)
                System.IO.Stream stream = _SaveFileDialog.OpenFile();
                byte[] FileContentsBytes = UTF8Encoding.UTF8.GetBytes(FileContentsTextBox.Text);
                stream.Write(FileContentsBytes, 0, FileContentsBytes.Length);

Hurrah, you just created a Silverlight based notepad yourself. Hope you will make something a little more useful than that out of this... Tongue

View Asad Ali's profile on LinkedIn

01-10-2013, 06:39 AM
Post: #2
RE: Reading and writing local files in Silverlight/C#
Thank you Asad for writing a very useful code snippet.. Keep posting.

[Image: 2604595733.png]

User(s) browsing this thread: 1 Guest(s)