When it comes to obtaining essential information about your systems, the SysInfo control and FileSystemObject provide everything necessary to get the job done. The SysInfo control lets you obtain important OS information without having to use API calls. The FileSystemObject provides access to the computer's file system so you can find out about available drives and files and create, move, and delete files or folders. In this article, I'll explain how you can get operating system and drive information by using a SysInfo control and FileSystemObject in your VB applications.
You can use SysInfo control to determine the operating system type and version, get the monitor size and resolution changes, detect plug-and-play devices, and find out the monitor battery and power status. The control also allows responding to some system-generated events. To use the control, you have to put it on a form and set its properties.
FileSystemObject is useful when you want to determine whether a file or a directory exists in the system. You can also use it to create, move, and delete files. You can use its Drives property to get information about available drives. To use FileSystemObject, you have to go to Project References and select the Microsoft Scripting Runtime option.
How does this work?
Let's look at a sample project to see what you can do with a SysInfo control and FileSystemObject. The project uses a SysInfo control to display the OS platform and version on a form, and it uses FileSystemObject to display available drives in a combo box and provide information about each one when it's selected. Listing A shows the code for this project.
In the general declaration section, we create a reference to FileSystemObject and Drives on the system so we can use them later. In the Form Load event, we create a loop to go through all available drives (Drvs) that we get from the Drives property of FileSystemObject (Fs) and check whether a particular drive is a network drive. If it is, we get the drive's share name; otherwise, we get the drive's volume name. We then add all available drive names to the combo box CboDrives. We also determine the OS platform and version from the OSPlatform property of the SysInfo control (SysInfo1) and assign it to the caption property of the lblOS label control to display this information the top of the form.
On the Click event of the combo box CboDrives, we start by removing information from all of the TextBox controls on the form by calling the CleanAll sub. In the CleanAll sub, we create a loop to go through all controls on the form and check their type. If the type of the control is a TextBox, we set its Text property to an empty string. After cleaning all the TextBox controls on the form, we figure out which drive is currently selected and get information about that drive by calling the GetDriveInfo sub and passing a currently selected drive to it in the statement Call GetDriveInfo (CurrDrv).
In GetDriveInfo, we determine the type of the drive passed (Unknown, Removable, Fixed, Remote, CDRom, or RamDisk) using the DriveType property of a CurrDrv object. We put the information about the drive on the form by assigning the specific details about the drive to corresponding TextBox controls (TxtDriveType, TxtSerialNumber, TxtAvailableSpace, TxtFileSystem, TxtFreeSpace, TxtTotalSize). In the error-handling routine, we display error information in case an error occurs, unless it's error number 71 (Disk Not Ready error), which means that there is no disk in the removable drive, usually your A: drive. If there is no disk in the drive specified, we don't want to alert our users to the error. So we use the Resume Next statement to skip the error and go forward with the rest of the code.
Download the code included in this article
The code in action
When you run the project, you should see the OS information on the top of the screen and a combo box with all available drives in the middle. When you click on the combo box to select a different drive, you should see information about that drive.