Wednesday, September 29, 2010

Computer class in Microsoft.VisualBasic.Devices

In the namespace Microsoft.VisualBasic.Devices there is the class Computer, which provides some very interesting functions. Most of them can probably be implemented differently too (with "real" C# functions), still I want to have a look at this class in this post out of curiosity.
To be able to use the class, a reference to Microsoft.VisualBasic (Project - Add Reference, tab .Net) has to be added and this has to be included by using Microsoft.VisualBasic.Devices.
The Computer class can be used to manipulate objects, which concern the own computer, the scope of application ranges from Audio over keyboard and mouse to ports.
Here some examples, what can be done with it:

Playing .wav files (the "C# variant" was describedi in this post):

// using Microsoft.VisualBasic; has to be included for this example as well!
Computer computer = new Computer();
computer.Audio.Play("pfad zur .wav - Datei", AudioPlayMode.Background);

The function Play() expects as first parameter the path to the file to be played, and as second one the playmode.
The function used here plays the sound in the background while the program continues running. Furtheremore there are the modes WaitToComplete (the program waits for the sound) and BackgroundLoop (the sound is repeatedly played in the background).

Readout current time and date (see this post for an alternative):

Computer ThisComputer = new Computer();
MessageBox.Show(ThisComputer.Clock.LocalTime.ToString());

Determine RAM size:

Computer ThisComputer = new Computer();
ulong RAMSize = ThisComputer.Info.TotalPhysicalMemory;

No comments:

Post a Comment