Introduce MY Namespace to Visual Basic (2)

zhaozj2021-02-11  187

My.computer

Another function block of My is a Computer object. My.Computer objects allow you to access the information of the hardware platform where the system underlying and the application is located. The second tab of the sample application demonstrates the interesting properties 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.addmyComputeRow ("Tick Count", _

My.computer.clock.tickcount)

MyAppds.mycomputer.addmyComputeRrow ("GMT Time", _

My.computer.eventlogs.Item ("Application"). Readentries (). Item (1) .message)

MyAppds.myComputer.addmyComputeRow ("CRTL Key Down", _

My.computer.keyboard.ctrlkeyDown.toString ())

MyAppds.mycomputer.addmyComputeRow ("Button Count", _

My.computer.mouse.buttoncount)

MyAppds.mycomputer.addmyComputeRow ("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.addmyComputeRow ("Wheel EXISTS", _

My.computer.mouse.wheelexists)

MyAppds.mycomputer.addmyComputeRow ("Computer Name", _

My.computer.name)

MyAppds.mycomputer.addmyComputeRrow ("Connection Status", _

My.computer.neetwork.connectionStatus)

MyAppds.mycomputer.addmyComputeRrow ("Connection Status", _

My.computer.neetwork.ipaddresses (0) .tostring ())

MyAppds.mycomputer.addmyComputeRow ("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.addmyComputeRow ("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 of the grid control in the MY.Application tab. Use a type of dataset to load the value obtained in the My.Computer object property and bind to a table control.

The element of the My.Computer object is not available in the table control. The items listed in the table below include attributes or methods that are not displayed in the table control.

My.computer

Attribute / method description

Clock allows you to access the current time zone time and local time, and 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 copy files, directories, mobile files, directories, read, or write files. The general situation only needs one line of code. Keyboard allows you to get the status of certain cases on the current keyboard. For example, if you get the CTRL key, the Shift key, or the ATL button is pressed, or if the CAPS LOCK / SCROLL LOCK is in an open state. Mouse allows you to get the state of the mouse and the specific hardware features it come with, if there are several buttons, whether it is rolling, etc. Name tells you the name of the local computer that is currently running. Network allows you to access the IP address information of the local computer, the connection status of the local computer, and the function of ping an address. Operating System allows you to access the local computer platform and version of information. Registry allows you to access the registry and provide the ability to read and write registry. Screen allows you to access the system attached to the monitor and display properties such as BitsPixel and WorkingArea.

Related Applications in My.computer

My.computer provides you with a lot of access to the system underlying, you can use in many ways. For example, you can use the network attribute and its related ping, pingwithdetails method easily ping a network address.

Dim pingResult as microsoft.visualbasic.net.pingResult = _

My.computer.neetwork.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 () & vbrlf & _" ping Attempts: "& pingResult.PingtTempts.toString () & vbcrlf &_

"Ping successes:" & pingResult.pingsucces.tostring ()

Messagebox.show (pingdetails)

Else

Messagebox.show ("

Ping of www.3leaf.com Was Not Successful

)

END IF

The most critical part of this code is My.computer.neetwork.pingwithDetails (). This line code returns a pingResult object, which contains the data returned by the standard ping action. If you have a minimum of ping operations, the average response time, the number of bytes, the number of the total ping, and the number of success are displayed in a message box. My.computer.network also provides a simplified version of Ping, only Returns True or False to show the success of ping. The function like this ping allows you to easily determine if you can formally establish communication with the server. By using My.computer.Network.ConnectionStatus, you can easily determine the status of the current network connection. The ConnectionStatus property returns True or False based on whether the computer has a network connection now.

MY.computer Another good application is when you need to access the file system. My.computer.FileSystem provides you better access to the file system, the code required to achieve fewer than VB.NET. With my.computer.filesystem, you can complete the following tasks with one line of code.

· Additional text after the file.

· Read all text from the file.

• Create all the parent folders required to create all the full paths when copying the folder.

• Create the parent folder required for all full paths when moving folders.

You can also simplify folders, files, and drives of the types in Microsoft.visualBasic.fileio namespace. These types are generally the types of the attributes and methods of my.computer.FileSystem objects.

The following example is all code required to add text at the end of the file.

My.computer.FileSystem.AppendText (Filepath, "Hello from" & _ "My.computer.FileSystem")

If you want to read a full text of a file, you only need the following code to do it.

My.computer.Filesystem.ReadAllText (FilePath)

Copy and moving folders are very easy.

My.computer.FileSystem.copyfolder (SourcePath, TargetPath, True, True)

Above this line of code copies the folder from SourcePath to TargetPath. The last two Boolean parameters specify whether TargetPath wants to overwrite the existing folder and the desired parent folder required to create.

Many properties and methods just mentioned by the FileSystem object can return to Drive, Folder, or File type objects. These Microsoft.visualBasic.Fileio namespaces have enabled many new properties and methods to make general programming work simplify. For example, you can use a row code to get the drive in the "I have Document" folder.

My.computer.filesystem.SpecialFolders.MyDocuments.Drive.driveName

More importantly, you can get the disk space remaining in the drive of the "I have a document" folder. My.computer.filesystem.SpecialFolders.MyDocuments.Drive.Freespace

The operation of a specific file or folder is equally easy to use file objects. For example, you can rename the name file with a line of code.

My.computer.filesystem.SpecialFolders.MyDocuments.Drive.Freespace

My.user

The next function block of the MY namespace is the User object. My.user object allows you to get information such as user names, domain names, and query the groups they belong to.

image 3

MY.User tab

The code for the control 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. Everything you have to do is to access the properties of the User object. The User object is composed of the following attributes or methods.

My.user

Attribute / method description

DisplayName allows you to access the display name of the current login user. DomainName allows you to access the domain name to which you are currently logged in, if he belongs to a domain. Isinrole allows you to check if the user belongs to a specific role. UserName allows you to access the username of the current login user. WindowsRole WindowsRole is a collection of all roles / group information that is currently locked by the current login user account.

Related applications in MY.User

My.user can give you quite a few information about the current login user. In many ways, the User objects provided by My are one of the perfect shortcuts of learning or using the .NET Framework.

In previous versions of .NET Framework, if you want to access the features provided similar to my.user, you must write the following so many code.

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 way

Messagebox.show (system.threading.thread.currentprincipal.Identity.name)

However, it will become so intuitive after using My.

My.user.username

This is a classic example that simplifies the .NET Framework operation with My. Many developers have facing Framework for the first time, they do not instinctively think of the account name of the user who wants to get the currently logged in user from the Principal object or from the current thread. Many developers think that this information is definitely available at a higher level of abstraction. Now use my, get this information is simple and fast.

My.webservices and my.forms

Next, you will have two main functions of My namespace, which are My.WebServices and My.Forms. Before entering this section, we will first make a short outline.

My.WebServices allows you to access the Web services referenced in the project as simple as the data source. Dgorders.DataSource = _

My.webservices.northwind.getorders (). Tables ("ORDERS")

If you use .NET Framework 1.0 / 1.1, you must create an instance of a WebService. Features provided with this My.Webservices can quickly access all Web services added to the web reference.

The last focus My.Form will bring back a signature programming style that appears in the Visual Basic desktop program and temporarily disappears in VB.NET. If you have developed with VB6, you will definitely know the following code, if your project needs to display a form:

FORM2.SHOW

In the vb.net era, you can't use this simple grammar. You can only display a form with the following code in VB.NET.

DIM FRM1 AS New Form1 ()

FRM1.SHOW ()

Using My.Forms, all forms in the project becomes a member of the Forms collection. Also if you want to access a form default instance, you don't need to use My.Forms at all. That is to say, the following code is equivalent.

My.Forms.Form2.show ()

Form2.show ()

In the next chapter, you will learn more of this method to display the function of the form (the translation: Unfortunately we can't see it).

If you want to see some MY.WEBSERVICES and MY.FORMS, you can look at the sample program related tab. All of these works will be described in detail behind the book. (This document is just the style of this book, so you are here, I want to see it, please go to the Amazon bookstore to buy this book)

in conclusion

MY Namespace has four functions for a developer. First it provides a lot of features that have not been previously versioned .NET Framework. Second, it provides a lot of shortcuts for the BCL of the .NET Framework. Third, in a certain extent, it allows you to operate the word "computer" as a logic entity when you think about the problem. Fourth, it allows some of your familiar old grams to return to Visual Basic, such as Form1.Show.

转载请注明原文地址:https://www.9cbs.com/read-5113.html

New Post(0)