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.
From the shown Reference Manager dialog, select Microsoft.VisualBasic and click the check-box as shown in the image below. Click OK.
Now you will be able to see the Microsoft.VisualBasic namespace in your reference list and ready to invoke Devices class functionality.
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.
- using System;
- using Microsoft.VisualBasic.Devices;
-
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- Computer myComputer = new Computer();
- Console.WriteLine("Computer name: {0} ", myComputer.Name);
-
- ComputerInfo objCI = new ComputerInfo();
- Console.WriteLine("OS Full Name: {0} ", objCI.OSFullName);
-
- Keyboard kBrd = new Keyboard();
- Console.WriteLine("CAPS is : {0}",kBrd.CapsLock);
-
- Mouse mus = new Mouse();
- Console.WriteLine("Mouse Wheel Exists : {0}", mus.WheelExists);
-
- if (myComputer.Network.IsAvailable)
- {
- Console.WriteLine("Computer is connected to network.");
- }
- else
- {
- Console.WriteLine("Computer is not connected to network.");
- }
-
- Console.WriteLine("Can Ping www.MyPassionFor.Net : {0}",myComputer.Network.Ping("www.myPassionFor.Net"));
- }
- }
- }
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
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.
- using System;
- using System.Diagnostics;
-
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Process process = new Process();
- process.StartInfo.FileName = "Iexplore.exe";
- process.StartInfo.Arguments = "www.youtube.com/watch?v=gCHoBJf4htg";
- process.Start();
- }
- }
- }
Successful execution of the program loads the IE with URL and opens the WebSite as shown in the image below.
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?
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.
- using Microsoft.VisualBasic.Devices;
- using System.Windows.Forms;
-
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Computer myComputer = new Computer();
-
- if (myComputer.Network.IsAvailable)
- {
- System.Diagnostics.Process process = new System.Diagnostics.Process();
- process.StartInfo.FileName = "Iexplore.exe";
- process.StartInfo.Arguments = "www.youtube.com/watch?v=gCHoBJf4htg";
- process.Start();
- }
- else
- {
- MessageBox.Show("You are NOT connected to Network", "Connection Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
Let's re-run this program with “No Network Connection” again and see the behavior.
Now if you will re-run this program with Network Connection then Web Site will open.