"A useful but neglected namespace: Microsoft.VisualBasic":

Source: Internet
Author: User

When you see this namespace, do not because it is VB things in a hurry to shut down the page, it will be your loss, The resources in this namespace were originally created to simplify vb.net development, so Microsoft.VisualBasic does not belong to the System namespace, but exists independently. Although it is built for VB, it does not prevent us from using it in C #.

Microsoft.VisualBasic namespace resources, can help us to easily and quickly practical use of some common computer software/hardware and network resources, improve the efficiency of development.

For the use of local computer resources, we may focus on the Microsoft.visualbasic.devices namespace, which contains resources related to native devices and operating systems.

Using these resources, we must add their references to the project. You can select "Microsoft.VisualBasic" on the ". NET" page from the Menu "project", "Add Reference", and then "OK" to finish. Then, don't forget to use them in your code files.

Here we focus primarily on the resources in the Microsoft.visualbasic.devices namespace, which mainly include:

  • Audio class
  • Clock class
  • Computer class
  • ComputerInfo class
  • Keyboard class
  • Mouse class
  • Network class
  • Ports class
  • Servercomputer class

For a detailed description of these resources, you can refer to the MSDN Library. Below, we'll look at how these resources are used in C # code through several typical applications.

Get operating system and memory information

To implement this feature, we used the ComputerInfo class, which shows the system name and version of the current computer, as well as memory-related information.

Using Microsoft.VisualBasic;
using Microsoft.visualbasic.devices;
Namespace Microsoft.visualbasic_demo
{
    class program
    {
       //Convert bytes to megabytes
        static ulong Bytetomb (ULONG bytevalue)
        {
             return bytevalue/1048576;
       }
        static void Main (string[] args)
         {
            computerinfo mypcinfo = new ComputerInfo ();

            Console.WriteLine ("Operating system: {0}", Mypcinfo.osfullname);
            Console.WriteLine ("Version: {0}", mypcinfo.osversion);

            Console.WriteLine ("All physical memory: {0}",
                 Bytetomb ( Mypcinfo.totalphysicalmemory));
            Console.WriteLine ("All virtual memory: {0}",
                Bytetomb ( Mypcinfo.totalvirtualmemory));
            Console.WriteLine ("Available physical memory: {0}",
                Bytetomb ( Mypcinfo.availablephysicalmemory));
            Console.WriteLine ("Available virtual memory: {0}",
                Bytetomb ( mypcinfo.availablevirtualmemory));


Console.read ();
}
}
}

The results of the operation are as follows:

If your software has special requirements for operating system versions or memory, using this information can easily be used to determine the relevant information. If you need to get more information about windows, such as the computer name (computername), the current login name

(username), etc., can be obtained using the SystemInformation class located in the System.Windows.Forms namespace.

Play a waveform file

The audio class located under the Microsoft.visualbasic.devices namespace can help

This article from: Macaidong Blog Reprint please specify the source URL: http://www.makaidong.com

We quickly play the waveform audio file. Waveform files are lossless audio files that can be used on many occasions, where system sounds in Windows systems are using waveform files.

In the audio class, we use the play () method primarily to play the waveform file. The code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using microsoft.visualbasic;using Microsoft.visualbasic.devices;namespace microsoft.visualbasic_demo{    class program    {        static void main ( String[] (args)        {            //play voice            audio Myaudio = new audio ();            Myaudio.play (@ "C:\test.wav", audioplaymode.background);            Console.read ();}}}    

The code is simple enough to play the "test.wav" waveform file in "C: \". If the file does not exist or the format is not supported, an exception is generated, so in the official code you should use the TRY-CATCH statement structure.

The two parameters of the play () method, the first one is the path where the file needs to be played. The second parameter specifies the play mode (mode), which is specified by the AudioPlayMode enumeration type and has the following main options:

    • Background, plays the sound in the background. This is the default value, and the above code can be written directly as "
      Myaudio.play (@ "c:\test.wav"); ".
    • Backgroundloop, looping through the background until the stop () method is called.
    • Waittocomplete, play the sound until the sound playback is complete before continuing to execute other code.
Basic Application of Network

Sometimes you need to determine whether the network is unblocked in the software, or if you upload or download files, you can use the Microsoft.visualbasic.devices namespace's network class.

The network class includes only the following common resources:

  • Isavaiable property to determine if the computer is networked, this property returns True if the network is unblocked, otherwise,
    returns FALSE.
  • Ping () method, which is used to test whether the connection to another computer is unblocked. The parameter can be a URL, a computer name, or an IP address.
  • DownloadFile () method to download the file from the specified network location. In general, the first parameter specifies the network location where the file needs to be downloaded, and the second parameter specifies the local storage location.
  • UploadFile () method to upload a file to the specified network location. In general, the first parameter specifies the local location where the file needs to be uploaded, and the second parameter specifies the network location to upload.

Search this article related to: a useful but ignored namespace: Microsoft.VisualBasic

This article links: http://www.makaidong.com/IT%E5%8D%9A%E5%AE%A2%E5%9B%AD/35753.shtml

Reprint please specify the source: a practical but ignored namespace: Microsoft.VisualBasic-Blog Park

"A useful but neglected namespace: Microsoft.VisualBasic":

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.