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.
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 = _
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.
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.
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.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
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.
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.
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.