Silverlight 4 – Local File Access

In Silverlight 4  apps which run with Elevated Trust can access local files by using System.IO and related types that would otherwise be unavailable. This allows apps  to access user files directly without recourse to the OpenFileDialog and SaveFileDialog classes. However,  access is limited to the the MyDocuments, MyPictures,MyMusic,  and MyVideos folders.

Use the local file access APIs in the same way as the  desktop framework, but use the values of the System.Environment..SpecialFolder enumeration to create the paths. For example:

private void TestLocalFileAccess()
{
    if (Application.Current.HasElevatedPermissions)
    {
        string myDocuments = Environment.GetFolderPath(
            Environment.SpecialFolder.MyDocuments);
        string filenamestr = "testfile.txt";
        string pathstr = System.IO.Path.Combine(myDocuments, filenamestr);
        if (File.Exists(pathstr))
        {
            string contents = File.ReadAllText(pathstr);
            MessageBox.Show(contents);
        }
    }
}
This entry was posted in FAQ and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>