Categories
DotNet Microsoft Silverlight Tips

Silverlight 2.0: OpenFileDialog, et. al.

Silverlight 2.0 enables developers to open and read local files outside the Isolated Storage. I’ve tried out the examples available on the internet but most examples are made with the pre-Release To Web (RTW) version of Silverlight 2.0 and I made some examples on how to use this feature with the RTW version.

Typically, prompting the OpenFileDialog is triggered with a button click and the code snippet that you will be seeing below is a typical OpenFileDialog scenario in a Silverlight 2.0 application.


private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofdSample = new OpenFileDialog();
ofdSample.Multiselect = true;

// You can specify the file filters that you want to use.
// For example, if you want to additional files to filter
// you may use the following:
// ofdSample.Filter = "Text files (*.txt;*.xml)|*.txt;*.xml";

ofdSample.Filter = "Text Files (*.txt)|*.txt";

// The example: if (ofdSample.ShowDialog() == DialogResult.OK)
// won't work with RTW version of Silverlight 2.0

if (ofdSample.ShowDialog() == true)
{
// Suppose we enabled multi-file select (see line #4 of this snippet),
// we can get the total number of files by doing the following:

int ofdNumSelectedFiles;
ofdNumSelectedFiles = ofdSample.Files.Count(fi => fi.Name.Trim() != "");

// If in case you want to loop through the various files selected,
// a cast is needed to get the FileInfo properties of each file selected.

FileInfo[] ofdFiles;
ofdFiles = (FileInfo[])ofdSample.Files;

// An example of looping through the files.
for (int fileCounter = 0; fileCounter < ofdNumSelectedFiles; fileCounter++)
{
MessageBox.Show(ofdFiles[fileCounter].Name);
}

// Just in case you want to validate the number of
// files selected IF MultiSelect is set to true
MessageBox.Show(ofdNumSelectedFiles.ToString());

// Sometimes, we want to load text files into Silverlight
// controls such as a TextBox. Suppose I have a Silverlight
// control named "txtDescription" and I want to load the
// contents of the text file into the control, I will be
// using the following lines.

// Notice that I placed it inside a try-catch block. This
// is to ensure that there are no other processes that uses
// the file.

try
{
using (StreamReader reader = ofdSample.File.OpenText())
{
txtDescription.Text = reader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show("File is inaccessible. Error returned was: " + ex.Message, "File Access Error", MessageBoxButton.OK);
}
}
}

If you would notice on line #33, I used the “Name” property of the FileInfo class. In this part of the code, you can do manipulations such as storing the file in the database (as most implementations, we just store the filename in the database while the actual file might reside in an IsolatedStorage or somewhere in the web application where the Silverlight application is hosted).  If you are a bit lost, Visual Studio provides intellisense with the properties and methods that you want to use:

intellisense

Hope this helps! Happy Coding!

Categories
Microsoft Social Networking Technology Windows Live

Can’t Install Windows Live Wave 3?

livex

I had the chance of getting the full offline installer of Windows Live products last night and I planned to upgrade all my installations of Windows Live products at home and at the office. It took me a sweet upgrade for the desktop and laptop at home unfortunately when I tried upgrading the products in my workstation, it gave me an installation error of 80004004 for Windows Live Messenger and some errors again on the other products.

livex

I am only particular with Windows Live Messenger and it’s a pain not to use that program on a daily basis. Seems like the error number that I have been encountering is not particular to Windows Live Messenger installation and it’s been associated with ActiveSync errors as well.

Upon further analysis, seems like the installation registries of previous Windows Live Products have been hindering the completion of the installation of the Wave 3 products. As a fix I removed all installation registry information for all previously installed Windows Live Products (well, I have no choice). If you can’t remove it via Add/Remove programs (or ctrl+r then type appwiz.cpl), you may use the Windows Installer Clean-Up Utility to force uninstallation of the products you have to remove.

After that, I was able to install the Windows Live Wave 3 products smoothly. Such a tedious task for a simple upgrade process.

Categories
ASP.NET DotNet Microsoft PhiNUG Web Development

Get Ready For The Face of the New Web

webrampup