Saturday, August 28, 2010

Reading Harddisk Information Using Silverlight 4 Out Of Browser Application

To read the hard disk information in silverlight was my question which I have search on net and was keen to do that, because I want to learn the silverlight. So after search on internet and searching the msdn for this question here is what I have found. You can see the out of the sample code in the Image 1, Here in the Image 1 I have display the hard disk partition just like the window 7 , the title , the progress bar to indicate the usage of the partition space and the at the end the summary of the the partition size.

Image 1

Now let us start with the code sample. First you have to create silverlight out of browser application for this you can click here to read how to create an out of browser application. From the link you can learn how to create an out of browser application in detail.Below is my code which is used to get the drive information. Here you can see that I have called the AutomationFactory.CreateObject, and passing the progID to the CreateObject.The AutomationFactory Provides access to registered Automation servers.Automation is a Windows-based technology that applications use to expose functionality to scripting tools and other applications. For example, you can use Automation to add Office features to your Silverlight-based applications.
An application or component exposing functionality is called an Automation server; an application accessing the functionality is called an Automation client. Because Automation servers must be preinstalled and run in full trust, Silverlight enables only trusted applications to serve as Automation clients.

dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject"); dynamic drives = fileSystem.Drives; DriveInformationCollection = new List≶DriveInformation>(); foreach (var drive in drives) { try { if (drive.IsReady) DriveInformationCollection.Add(new DriveInformation { VolumeName = drive.VolumeName, DriveLetter = drive.DriveLetter, TotalSpace = drive.TotalSize, FreeSpace = drive.FreeSpace, FileSystem = drive.FileSystem, DriveType = (eDriveType)Enum.ToObject(typeof(eDriveType), drive.DriveType) }); } catch (COMException Ex) { string strMessage = Ex.Message; } }

Now we have the reference of the automation server which is store in fileSystem of type dynamic.Dynamic is a new type provided by framework 4, you can get more information about the dynamic type by clicking here. Next I have call the drives object of the fileSystem to store the drive in new dynamic object. And then I have used to foreach loop to get the drive information to be store in the class which I have created with the name DriveInformation name.The Drive object allows you to gain information about the various drives attached to a system, either physically or over a network. Its properties allow you to obtain information about:
  • The total size of the drive in bytes (TotalSize property)
  • How much space is available on the drive in bytes (AvailableSpace or FreeSpace properties)
  • What letter is assigned to the drive (DriveLetter property)
  • What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM disk (DriveType property)
  • The drive's serial number (SerialNumber property)
  • The type of file system the drive uses, such as FAT, FAT32, NTFS, and so forth (FileSystem property)
  • Whether a drive is available for use (IsReady property)
  • The name of the share and/or volume (ShareName and VolumeName properties)
  • The path or root folder of the drive (Path and RootFolder properties)
In the view of the form. I have used the list box control to display the drive information , which consist of the text block and the progress bar control.For progress bar control I have added two ChangePropertyAction which you can here for more detail. The condition for first ChangePropertyAction is to check the value of the progress bar if the value is greater then or equal to 80 then the progress bar will be shown red and if the value of the progress bar is less then 80 then progress bar will be shown normally. And the bottom of the user control I have used the grid layout control, the data context of the grid control is set to the selected Item of the list box control. When the selection of the list box changed the correspondent detail of the selected item will be displayed at the bottom of the user control.You can download the source code from here


1- Automation Class
2- Working with Drives and Folders

All and any comments / bugs / suggestions are welcomed!

No comments: