Chapter 14. Reading and Writing Local Files

One of the (many) things HTML cannot do is read arbitrary files from the client’s machine or even create or delete such files. Rightfully so, you may say, since severe security precautions are involved. However, there are some scenarios where at least a limited read or write access is desirable. Imagine a Silverlight support application that allows users to upload error logs to the server that then processes this data. Or imagine a highly personalized, Silverlight-driven web portal that wants to save user settings on the client machine. In such cases, Silverlight provides two techniques that will help you implement a good solution for those scenarios. Just keep in mind that there are several security precautions in place. So as a user, don’t worry, but as a developer, be prepared to take a few extra things into consideration when implementing file access.

Accessing Local Files

For obvious security reasons, Silverlight does not allow direct access to files on the client’s machine. However, in combination with the OpenFileDialog class, you can get read access in the form of a FileStream object. Users do have to specifically grant access to files, and the only data the Silverlight application receives is the data from the files, not any meta information. In our sample application, we will allow users to select a video file from their hard drives and grant Silverlight access to it; the video is then shown as part of the application.

Let’s start ...

Get Essential Silverlight 2 Up-to-Date now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.