Introduce my namespace to Visual Basic programmers (ii)

Source: Internet
Author: User
Tags count current time file system local time thread tostring versions web services
visual| Program | programmer | Namespace My.Computer
Another feature block for my is the computer object. The My.Computer object allows you to access information on the underlying system and the hardware platform where the application resides. The second tab of the sample application shows the interesting attributes you can find in the My.Computer object.



Figure Three

My.Computer tab

The code that controls this grid is as follows:

MyAppDS.MyComputer.AddMyComputerRow ("Clock", _

My.Computer.Clock.LocalTime)

MyAppDS.MyComputer.AddMyComputerRow ("GMT Time", _

My.Computer.Clock.GmtTime)

MyAppDS.MyComputer.AddMyComputerRow ("Tick Count", _

My.Computer.Clock.TickCount)

MyAppDS.MyComputer.AddMyComputerRow ("GMT Time", _

My.Computer.EventLogs.Item ("Application"). Readentries (). Item (1). Message)

MyAppDS.MyComputer.AddMyComputerRow ("Crtl Key Down", _

My.Computer.Keyboard.CtrlKeyDown.ToString ())

MyAppDS.MyComputer.AddMyComputerRow ("button Count", _

My.Computer.Mouse.ButtonCount)

MyAppDS.MyComputer.AddMyComputerRow ("Buttons swapped", _

My.Computer.Mouse.ButtonsSwapped)

MyAppDS.MyComputer.AddMyComputerRow ("Mouse Exists", _

My.Computer.Mouse.Exists.ToString ())

MyAppDS.MyComputer.AddMyComputerRow ("Wheel Scroll Lines", _

My.Computer.Mouse.WheelScrollLines.ToString ())

MyAppDS.MyComputer.AddMyComputerRow ("Wheel Exists", _

My.Computer.Mouse.WheelExists)

MyAppDS.MyComputer.AddMyComputerRow ("Computer Name", _

My.Computer.Name)

MyAppDS.MyComputer.AddMyComputerRow ("Connection Status", _

My.Computer.Network.ConnectionStatus)

MyAppDS.MyComputer.AddMyComputerRow ("Connection Status", _

My.Computer.Network.IPAddresses (0). ToString ())

MyAppDS.MyComputer.AddMyComputerRow ("Operating System Platform", _

My.Computer.OperatingSystem.Platform)

MyAppDS.MyComputer.AddMyComputerRow ("Version", _

My.Computer.OperatingSystem.Version.Major & "." & _

My.Computer.OperatingSystem.Version.Minor & "." & _

My.Computer.OperatingSystem.Version.Revision & "." & _

My.Computer.OperatingSystem.Version.Build)

MyAppDS.MyComputer.AddMyComputerRow ("Full Name HKEY_CURRENT_USER", _

My.Computer.Registry.CurrentUser.FullName)

MyAppDS.MyComputer.AddMyComputerRow ("Device Name", _

My.Computer.Screen.DeviceName)

This code is very similar to the code that controls the grid control in the My.Application tab. Loads the values obtained in the My.Computer object properties with a typed dataset and binds to a table control.

Not all elements of the My.Computer object can be displayed in a table control. The items listed in the following table include properties or methods that are not displayed in the table control.

My.Computer

Attribute/Method Description

Clock
Allows you to access the current time zone and local time, as well as the counting scale.

Eventlogs
Allows you to access local event log information and create or delete event logs.

FileSystem
Allows you to do many IO operations, such as copying files, directories, moving files, directories, reading or writing files, and so on. In general, only one line of code is required.

Keyboard
Allows you to get the status of certain cases on the current keyboard. For example, get the CTRL key, SHIFT key, or whether the ATL key is pressed, or caps Lock/scroll LOCK is turned on.

Mouse
Allows you to get the state of the mouse and the specific hardware features it comes with, such as a few buttons, whether it has roll theory, and so on.

Name
Tells you the name of the local computer that is currently running.

Network
Allows you to access the local computer's IP address information, the local computer's connection status, and the ability to ping an address.

Operating System
Allows you to access the local computer platform and version of the information.

Registry
Allows you to access the registry and provides the ability to read and write to the registry.

Screen
Allows you to access the monitors included with the system and display properties such as BitsPerPixel and Workingarea.


The related application of my.computer in development

My.Computer provides you with a lot of access to the underlying functionality of the system, and you can use it in many ways. For example, you can easily ping a network address with the Network property and its associated ping, pingwithdetails method.

Dim Pingresult as Microsoft.VisualBasic.Net.PingResult = _

My.Computer.Network.PingWithDetails ("www.3leaf.com")

If pingresult.pingsuccesses > 0 Then

Dim pingdetails as String = "Average Ping time in MS:" & _

Ingresult.averagepingtimeinmilliseconds & VbCrLf & _

"Bytes Received:" & PingResult.BytesReceived.ToString () & VbCrLf & _

"IP Address:" & PingResult.IPAddress.ToString () & VbCrLf &_

"Ping attempts:" & PingResult.PingAttempts.ToString () & VbCrLf & _

"Ping successes:" & PingResult.PingSuccesses.ToString ()

MessageBox.Show (Pingdetails)

Else

MessageBox.Show ("Ping of www.3leaf.com is not successful")

End If

The most critical part of this code is My.Computer.Network.PingWithDetails (). This line of code returns a Pingresult object that contains the data returned by the standard ping action. If at least one ping succeeds, the average response time, the number of bytes obtained, the total number of pings and the number of successes will be displayed in a message box. My.Computer.Network also provides a simplified version of Ping, only returns TRUE or false to ping success or not. The ability to ping an address like this allows you to easily determine whether you can formally establish communication with the server. By using My.Computer.Network.ConnectionStatus, you can also easily determine the status of the current network connection. The Connectionstatus property returns True or false based on whether the computer now has a network connection.

My.Computer Another good application is when you need to access the file system. My.Computer.FileSystem provides you with a better way to access the file system, with less code than vb.net. With My.Computer.FileSystem, you can do the following tasks in just one line of code.

· Appends text to the file.

· Reads all the text from the file.

· The parent folder that is required to create all the full paths when copying a folder.

· The parent folder that is required to create all the full paths when moving a folder.

You can also simplify the operation of folders, files, and drives with types in the Microsoft.VisualBasic.FileIO namespace. These types are generally the types that are returned by the properties and methods of the My.Computer.FileSystem object.

The following example is all the code required to append text to the end of the file with my.

My.Computer.FileSystem.AppendText (FilePath, "Hello from" & _
"My.Computer.FileSystem")

If you want to read all the text of a file, just the following code.

My.Computer.FileSystem.ReadAllText (FilePath)

Copying and moving folders becomes very easy.

My.Computer.FileSystem.CopyFolder (SourcePath, TargetPath, True, True)

This line of code copies the folder from SourcePath to TargetPath. The last two Boolean parameters specify whether TargetPath will overwrite the existing folder and specify whether the desired parent folder needs to be created.

Just mentioned that many of the properties and methods of the FileSystem object can return objects of drive, folder, or file type. The objects in these Microsoft.VisualBasic.FileIO namespaces encapsulate a number of new properties and methods that simplify the general programming effort. For example, you can use a line of code to get the "My Documents" folder in the drive.

My.Computer.FileSystem.SpecialFolders.MyDocuments.Drive.DriveName

More importantly, you can get the rest of the disk space on the drive where the My Documents folder resides.

My.Computer.FileSystem.SpecialFolders.MyDocuments.Drive.FreeSpace

It is also very easy to use a file object for a particular file or folder. For example, you can easily duplicate a file name with a single line of code.

My.Computer.FileSystem.SpecialFolders.MyDocuments.Drive.FreeSpace

My.User
The next feature block in my namespace is the user object. The My.User object allows you to obtain information such as user name, domain name, and the group of users who are currently logged in.



Figure 3

My.User tab

The code that controls the table control is as follows:

MyAppDS.MyUser.AddMyUserRow ("Display Name", _

My.User.DisplayName)

MyAppDS.MyUser.AddMyUserRow ("Domain Name", _

My.User.DomainName)

MyAppDS.MyUser.AddMyUserRow ("User Name", _

My.User.UserName)

MyAppDS.MyUser.AddMyUserRow ("Windows role", _

My.User.WindowsRoles (0). ToString ())

This code accesses the current user's information, which is very simple. All you have to do is access the properties of the user object. The user object is made up of the following properties or methods.

My.User

Attribute/Method Description

DisplayName
Allows you to access the display name of the currently logged-on user.

DomainName
Allows you to access the domain name that the current logged-on user belongs to, if he belongs to a domain.

IsInRole
Allows you to query whether the user belongs to a specific role.

UserName
Allows you to access the user name of the currently logged-on user.

Windowsrole
Windowsrole is a collection that contains all the role/group information that the current logged-on user account belongs to.


The related application of my.user in development

My.User can give you quite a lot of information about the current logged-in user. In many ways, the user object provided by my is one of the best shortcuts for learning or using the. NET framework.

In previous versions of the. NET Framework, if you wanted to access the functionality provided by a similar my.user, you would have to write so much code below.

Imports System.Security.Principal

Imports System.Threading.Thread

...

Dim Winprin as WindowsPrincipal = Thread.CurrentPrincipal

MessageBox.Show (WinPrin.Identity.Name)

Or you have to write this.

MessageBox.Show (System.Threading.Thread.CurrentPrincipal.Identity.Name)

However, it becomes so intuitive after using my.

My.User.UserName

This is a classic example of simplifying the. NET framework operations with my. For the first time many developers face the framework, they do not instinctively think of getting the process identity from the principal object or from the current thread to get the account name of the currently logged-on user. Many developers believe that this information must be available at a higher level of abstraction. Now using my, getting this information is both simple and fast.

My.WebServices and My.Forms
Next you will learn the other two main features of my namespace, they are my.webservices and my.forms. Before entering this section, let's start with a brief overview.

My.WebServices allows you to access Web services referenced in your project with the same syntax as accessing the data source.

Dgorders.datasource = _

My.WebServices.Northwind.GetOrders (). Tables ("Orders")

If you are using the. NET Framework 1.0/1.1, you must create a WebService instance to achieve the functionality of the above line of code. This my.webservices provides the ability to quickly access all Web services that are added to a Web reference.

The final focus My.form will bring back a symbolic programming style that appears in the Visual Basic desktop program and temporarily disappears in vb.net. If you have ever developed with VB6, you must know the following code if your project needs to display a form:

Form2.show

In the vb.net era, you can no longer use this simple grammar. Only one form can be displayed in vb.net with the following code.

Dim Frm1 as New Form1 ()

Frm1. Show ()

With My.Forms, all forms in the project become members of the Forms collection. Also, if you want to access the default instance of a form, you don't need to use my.forms. That means the following code is equivalent.

My.Forms.Form2.Show ()

Form2.show ()

In the next chapters, you'll learn more about how to display the form in this way (but we can't see it).

If you want to see some examples of my.webservices and my.forms, you can look at the sample program-related tabs. All these workers can be described in detail behind the book. (This document is just a sample of this book, so that's it, so I'd like to see the book at the Amazon Bookstore later.)

Conclusion

The My namespace has four main features for a developer. First, it provides many features that are not available from previous versions of the. NET Framework. Second, it provides many shortcuts for fast access to the. NET Framework's BCL. Third, to a certain extent, it allows you to use the word "computer" as a logical entity when you think of a problem. Four, it allows you to return to some of the old grammar familiar to Visual Basic, such as form1.show.


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.