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:
File Explorer using Silverlight 4 COM Interoperability
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 files or folders present inside your system very easily (as you do it in other applications like Win Form, WPF etc.). Let's discuss more on it with a small example.
Case Study
Let's take a case study. One of your client asked you to create a Silverlight application, which will run outside the browser window and read the hosts 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.
...
...
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.
Read more: Kunal's Blog
0 comments:
Post a Comment