On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

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).