Click here to Skip to main content
Click here to Skip to main content

Silverlight 5 Features Local File System Access without using COM API

By , 8 Jun 2011
 

Do you know that you can now access local files and folders in Silverlight 5? Yes, you heard right. Before Silverlight 5, it was only limited to trusted locations like My Documents folder, which means it was only possible to read/write in My Documents. Now using Silverlight 5, you can do such operations in any file or folder.

Let's discuss more on this topic. After reading this post, you will be able to read/write to/from any file in local file system. Also, you will be able to get information about System Resources. Read to know more.

Background

In Silverlight 4, it was possible to only access files and folders present inside a fully trusted folder like My Documents. You could use COM APIs from fully trusted OOB Silverlight applications. If you want to know more about this topic, read this post:

In Silverlight 5, you don't need any COM APIs. If your Silverlight application is a trusted out-of-Browser application, you can access any file or folder present inside your system very easily (as you do it in other applications like WinForm, WPF, etc.). Let's discuss more about it with a small example.

Case Study

Let's take a case study. One of your clients asked you to create a Silverlight application, which will run outside the browser window and read the host's file present in the local system (i.e. c:\windows\system32\drivers\etc folder) and display it in the view.

To do this, you need to create an application using Silverlight 5. Set it as an Out-of-Browser application and mark it to run under "Elevated Trusted Application" from the project properties.

Setting Elevated Trust for Silverlight Out-of-Browser application

Once you setup your project properties, open the XAML file and add a button called "Read File" and attach a Click_Event to it. We will also add a ListBox so that we can display the text content inside it. Here is the code snippet of the same:

<UserControl x:Class="FileAccessDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button Width="150" Height="26" 
        Content="Read File" Click="Button_Click"/>
        <ListBox x:Name="lstContent"/>
    </StackPanel>
</UserControl>

Now we need to implement the Click event logic for the button. To do this, go to the code behind file and write your own logic to read any file present in your system. In our example, we will use the File class present under the System.IO namespace and read the host's file. Then, we will add the content in our ListBox.

Here is the code implementation:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string fileContent = File.ReadAllText(@"c:\windows\system32\drivers\etc\hosts");
    lstContent.Items.Add(fileContent);
}

It's so simple. Just call the File.ReadAllText() method and pass the complete file path as the parameter. This will read the content and store it in a local variable called "fileContent". Now add the content to our ListBox.

This is all about the code. If you run the application now (as OOB App), you will see the below Window in your screen:

image

Now click the button called "Read File" and you will see the content of your host's file as shown below (the content shown here will vary from system to system, but you will see the same content present in your file):

image

Hope this feature will help you to do various operations on your system. There are many things available that you can explore for file system access. If I get some more time, I will publish them for you to read. Till then... happy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMr kunal Pinmemberysnraju9 Jan '13 - 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid