MULTIMEDIA

Silverlight Recipes : Save a File Anywhere on the User's System

6/30/2012 3:04:10 PM

1. Problem

You want to allow the user to save a file anywhere on their system without the constraints of isolated storage.

2. Solution

Use the SaveFileDialog object to persist a file to the user's file system from within a user-initiated event handler, such as a button click or key press.

3. How It Works

Silverlight 3 introduced the new SaveFileDialog object, which allows the user to select a file location that the Silverlight application can save a file outside of isolated storage. Like the OpenFileDialog, the SaveFileDialog must be raised in an event handler resulting from user interaction such as a key press or button click. Once that's accomplished, using the SaveFileDialog is very straightforward.

Out-of-browser (OOB) elevated trust applications have more access to the file system and are not limited to isolated storage. 

4. The Code

The code presents a simple UI with a button that the user can click to bring up the SaveFileDialog object (see Figure 1).

Figure 1. Recipe 1 SaveFileDialog In Action

Just as with most any file-related dialog box, developers can configure the filter (e.g., .txt or .tiff) as well as the default file type. You are going to write out a simple text file, so you'll use Text Files (*.txt) has the source code that displays the SaveFileDialog object and writes out the file to the returned stream by calling the OpenFile() method.

Listing 1. Recipe 1. MainPage.xaml.cs File
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Ch02_ProgrammingModel.Recipe2_14
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void btnSaveFile_Click(object sender, RoutedEventArgs e)
    {
      SaveFileDialog sfd = new SaveFileDialog();
      sfd.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
      sfd.FilterIndex = 1;
      if (true == sfd.ShowDialog())
      {
        using (Stream fs = sfd.OpenFile())
        {
          byte[] textFileBytes = (new UTF8Encoding(true)).GetBytes(
          "Welcome to Silverlight 4!!!! \r\n\r\nYour Authors,\r\n\r\nRob and Jit");
          fs.Write(textFileBytes, 0, textFileBytes.Length);
          fs.Close();
        }
      }
    }
  }
}					  
Other  
  •  Silverlight Recipes : Managing Resources in Large Projects
  •  Video Codecs and File Formats Exposed (Part 3)
  •  Video Codecs and File Formats Exposed (Part 2) - WMV, MOV,MKV
  •  Video Codecs and File Formats Exposed (Part 1) - AVI, MPG
  •  How To Plan Every Element Of A Successful Studio Shoot
  •  How To Create A Window Light Effect And Much More (Part 2)
  •  How To Create A Window Light Effect And Much More (Part 1)
  •  CyberLink MediaSuite 10 Ultra Review
  •  Tasmania - Ideal Destination For Landscape Photographers (Part 2)
  •  Tasmania - Ideal Destination For Landscape Photographers (Part 1)
  •  Shoot Your Best-Ever Portraits (Part 3) - Location setups
  •  Shoot Your Best-Ever Portraits (Part 2) - Natural light setups
  •  Shoot Your Best-Ever Portraits (Part 1)
  •  Other Snippet Of Photography News From Across The Globe
  •  Change The View : Cut out subjects in Photoshop using the Colour Channels in four simple steps
  •  Home Theatre Pc Software And Operating Systems (Part 7) - Playing Back Blu-rays
  •  Home Theatre Pc Software And Operating Systems (Part 6) - Setting Up MediaPortal
  •  Home Theatre Pc Software And Operating Systems (Part 5) - MediaPortal
  •  Home Theatre Pc Software And Operating Systems (Part 4) - XBMC
  •  Home Theatre Pc Software And Operating Systems (Part 3) - Setting Up Windows Media Center
  •  
    Top 10
    Windows Vista : Installing and Running Applications - Launching Applications
    Windows Vista : Installing and Running Applications - Applications and the Registry, Understanding Application Compatibility
    Windows Vista : Installing and Running Applications - Practicing Safe Setups
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    Most View
    Designing and Implementing Mobility in Exchange Server 2010 : Working with ActiveSync Policies
    Slim And Slimmer (Part 2)
    SQL Server 2008 : Failover clustering - Clustering topologies and failover rules
    Buyer’s Guide - Keyboard and mice (Part 4) - GIGABYTE Aire M1, Gyration Air Mouse Elite, IOGEAR Phaser GME422RW6
    IUSR and IIS_USRS
    The Secret Processor Revolution (Part 1)
    Logitech Ue Boombox - Fantastic Sound Quality For The Money
    The 10 Things To Know Before Buying A Laptop (Part 3)
    Anthem MRX 700 – The Greatness Home Cinema Amplifier
    Joomla! 1.5 : Tracking and Tracing to Improve Your Web Site - Looking at your options (part 1)
    Protecting SharePoint with Advanced Antivirus and Edge Security Solutions : Securing SharePoint Sites Using Forefront UAG
    Expert Computing Advice – January 2013 (Part 2)
    D-Link Cloud Gigabit Router N600
    The Tablet Wars (Part 2) - Kindle Fire HD
    Guide To Upgrades With The Greatest Effects (Part 2)
    Exchange Server 2007: Design and Deploy Disaster Recovery Settings - Implement Database Portability
    Relief From Mobile Minutes Charges?
    Programming Windows Services with Microsoft Visual Basic 2008 : Extending the WMI Implementation
    What To Do With An Old Mac (Part 4)
    iTunes Match Q&A