How to: Access the Local File System in Trusted Applications


Trusted applications can access System.IO and related types that are otherwise unavailable. This enables you to access user files directly without using the OpenFileDialog and SaveFileDialog classes. However, in Silverlight 4 and earlier, you can access only files in user folders, specifically the MyDocuments, MyMusic, MyPictures, and MyVideos folders.

Note Note:

These folders are distinct from the Windows 7 Libraries named Documents, Music, Pictures, and Videos. These library folders typically combine the contents of the user folders with other folders, such as shared media folders. However, trusted applications cannot access non-user folders except through the OpenFileDialog and SaveFileDialog classes.

Starting in Silverlight 5, trusted applications have unrestricted access to the local file system, although in-browser trusted applications are still subject to browser security restrictions. For more information, see Trusted Applications.

The following topic demonstrates how to access files in user folders.

Note Note:

Silverlight for Windows Phone Silverlight for Windows Phone and Silverlight 3 do not support trusted applications.

To access files in user folders

  • Use the supported APIs as you would in the desktop framework, but use the previously-indicated values of the System.Environment.SpecialFolder enumeration to construct paths. The following example demonstrates the required usage.

    private void TestIO()
        if (Application.Current.HasElevatedPermissions)
            string myDocuments = Environment.GetFolderPath(
            string filename = "test.txt";
            string path = System.IO.Path.Combine(myDocuments, filename);
            if (File.Exists(path))
                string contents = File.ReadAllText(path);

For more examples, see the StreamReader, StreamWriter, and DirectoryInfo classes.

For security considerations in trusted applications, see Trusted Applications.

Community Additions