Real-World Scenario of Using VisualBasic.Devices With C#

Abstract

In this article I will share some real-world problems that appear to be complex, but with usage of Microsoft.VisualBasic.Devices those can be easily done.

Scenario

You are working on an application and you come across any of the following requirements before any action can possibly take place:

  • Verify OS name.
  • Make sure CAPS is ON or NOT (as per your application's need)
  • Check Network availability before you open a browser or load the site.
  • Does the mouse have a wheel?
  • Ability to Ping a URI or Computer's IP before your entire business logic starts executing.

Introduction

All of the preceding specified situations are valid and used in many real-world applications, the issue is that none of them appear straightforward to implement, as it involves:

  • Communication with Operating System; to know the OS name
  • Communication with Hardware; to know if CAPS is ON or OFF
  • Communication with Network Adaptor; to check if Computer is connected to Network on not
  • Communication with Hardware pointing device; Mouse has wheel or not
  • Perform some action over the network; Pinging a computer or URL

Solution

Microsoft.VisualBasic.Devices is a namespace that helps in such scenarios. The following code example shows the use of various classes.

In order to use Microsoft.VisualBasic.Devices namespace you must add a reference of it to your project/application.

Right-click the references and select Add Reference.

Add Reference

From the shown Reference Manager dialog, select Microsoft.VisualBasic and click the check-box as shown in the image below. Click OK.

Microsoft.VisualBasic
 
Now you will be able to see the Microsoft.VisualBasic namespace in your reference list and ready to invoke Devices class functionality.

VisualBasic namespace

Code Example

Let's use VisualBasic.Devices namespace to get some Computer System, Hardware and Network related information.

My current environment is as follows:

  • CAPS lock is Off
  • Mouse has a Wheel
  • Computer is connected to Network
  • URL (www.MyPassionFor.Net) is valid and can be Pinged

Let's get all this verified via VisualBasic.Devices namespace.

  1. using System;  
  2. using Microsoft.VisualBasic.Devices;  
  3.   
  4. namespace ConsoleApplication4  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Computer myComputer = new Computer();  
  11.             Console.WriteLine("Computer name: {0} ", myComputer.Name);              
  12.              
  13.             ComputerInfo objCI = new ComputerInfo();  
  14.             Console.WriteLine("OS Full Name: {0} ", objCI.OSFullName);  
  15.                      
  16.             Keyboard kBrd = new Keyboard();  
  17.             Console.WriteLine("CAPS is : {0}",kBrd.CapsLock);  
  18.   
  19.             Mouse mus = new Mouse();  
  20.             Console.WriteLine("Mouse Wheel Exists : {0}", mus.WheelExists);  
  21.               
  22.             if (myComputer.Network.IsAvailable)  
  23.             {  
  24.                 Console.WriteLine("Computer is connected to network.");  
  25.             }  
  26.             else  
  27.             {  
  28.                 Console.WriteLine("Computer is not connected to network.");  
  29.             }  
  30.   
  31. Console.WriteLine("Can Ping www.MyPassionFor.Net :  {0}",myComputer.Network.Ping("www.myPassionFor.Net"));  
  32.         }  
  33.     }  

Example 

Let's try with different settings.
  • CAPS lock is On
  • Mouse has a Wheel
  • Computer is NOT connected to Network
  • URL (www.MyPassionFor.Net) is valid and can NOT be Pinged due to NO Network Connection

Network Connection

You received an exception from Ping due to No network connection. 

Another Real-World Example

You are building an application that opens a given URL in Internet Explorer.

  1. using System;  
  2. using System.Diagnostics;  
  3.   
  4. namespace ConsoleApplication3  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Process process = new Process();  
  11.             process.StartInfo.FileName = "Iexplore.exe";  
  12.             process.StartInfo.Arguments = "www.youtube.com/watch?v=gCHoBJf4htg";  
  13.             process.Start();  
  14.         }  
  15.     }  

Successful execution of the program loads the IE with URL and opens the WebSite as shown in the image below.

program loads the IEt5rbg

As you can see this task is heavily dependent on “Network Connection” and if you are not connected then you don't have a good User Experience. Want to see how it will look like in reality?

Network Connection

So being a defensive programmer think of dependencies of the code execution and failure scenarios. In this case it will be wise to verify if the computer is connected to the network or not before Web Browser is opened with the given URL. This seems tedious but with VisualBasic.Devices it can be done with one line of code, that must be part of an if condition.

  1. using Microsoft.VisualBasic.Devices;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace ConsoleApplication3  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Computer myComputer = new Computer();  
  11.   
  12.             if (myComputer.Network.IsAvailable)  
  13.             {  
  14.                 System.Diagnostics.Process process = new System.Diagnostics.Process();  
  15.                 process.StartInfo.FileName = "Iexplore.exe";  
  16.                 process.StartInfo.Arguments = "www.youtube.com/watch?v=gCHoBJf4htg";  
  17.                 process.Start();  
  18.             }  
  19.             else  
  20.             {  
  21.                 MessageBox.Show("You are NOT connected to Network""Connection Error",   
  22.                 MessageBoxButtons.OK, MessageBoxIcon.Error);  
  23.             }  
  24.         }  
  25.     }  

Let's re-run this program with “No Network Connection” again and see the behavior.

No Network Connection

Now if you will re-run this program with Network Connection then Web Site will open.


Similar Articles