Introduce MY Namespaces (1) to Visual Basic programmers (1)

zhaozj2021-02-11  227

Excerpted from "Microsoft Visual Basic Programmer's Introduction to Whidbey" by Sean Campbell, Scott Swigart, Kris Horrocks, Derek Hatchard, and Peter Bernhardt.

Publisher, Microsoft Press, ISBN # 0-7356-2058-x. © 2004, Microsoft Corporation

Application: my namespace

new concept

When you write an application with VB6, you have used VB's runtime, which contains a large batch of COM objects; and Win32 APIs used by WINDOWS. The original version of the .NET Framework integrates most of these content into a huge library, which is our familiar base class library (bcl). Some of the BCL support access to the underlying level, some classes allow you to easily access computer hardware information, and some can make you network communications, encrypted data, and access registry, etc. in different applications. If you want to make a thorough .NET developer, you have to master BCL and the rich features it offer. If the BCL lacks enough understanding, developers will make a lot of unnecessary duplicate labor and re-build some already existing classes. Sometimes some of the features provided by .NET "hide" is too deep, or relative to the huge .NET Framework is too inconsistent. There are many people who write an article to implement the functions of their ancestral algorithms, in fact, can be used very easily with certain classes in Framework, such as Path or Passwordderivebyes. In fact, many people will tell you to learn .Net is actually not learning Visual Basic or C #, but learning BCL. However, learning BCL is also a huge challenge because it is too big. Some very common classes are mixed with those categories that can not be used. To make it easier to find the most common class in those frames, Visual Basic 2005 now provides a "shortcut" namespace - MY. You can use MY to access your computer, applications, and user information, you can use it to access forms and web services. It should be noted that MY can only be used when using Visual Basic 2005, and it is not possible to directly access MY when using C #. It is necessary to mention that MY namespace is not just a shortcut. In some cases, the class provided by my namespace is more powerful than the class features you get in a large number of namespaces in BCL. For example, the Folder object provides more properties, such as Drive, which does not exist at the System.io.DirectoryInfo class. My also allows Microsoft think about "computer", "app" or "user" as a complete entity, and self-assessment "What should I do for a 'computer" What should I do ". The results of thinking include my.computer.neetwork.ping, which allows you to use a line of code to implement the PING operation of the network computer. The function you use now has only called the COM library, or even the win32 API can do it, and now it can easily find and use it in my.

MY Namespace below is divided into the following area:

MY object model

Object description

My.Application contains information currently running applications, such as title, current path, and versions. It can also access environment variables to make you easily write local application logs or custom logs. My.computer provides information on the local computer hardware or system under application. For example, registry, printer, keyboard, real device, etc. These are the objects you can access through this class. My.user contains information about the current user, including the user display name, domain name, group name, etc. My.Webservices It allows you to easily access Web services that have been added to the project. MY.FORMS Collection of a form that exists in a current project. You can display it, hide these forms without explicitly create their objects in your code. (Translation: This is great, VB6 programmer's gospel) is as you can see, MY namespace gives you a convenient way, allowing you to directly access those common functions directly in your daily development.

Go deep into code

In this section, I will pass a short example, take you into the MY namespace. This example explores the world in My in a certain depth. This program has a lot of tabs, and each tab focuses on the main functionality of My.

figure 1

My.Application tab.

Image modification - you need to delete my.datasources - this part has already been removed.

My.Application

The first tab - MY.Application has a data table control, which displays most of the attribute values ​​for my.Application objects. The code controls this data table control is as follows:

CLRVERSIONINUSE has already been removed (you can see it in M2 Bits, but have been removed)

MyAppds.myApplication.AddmyApplicationRow ("CLR Version in Use", _

My.application.clrversioninuse.major.toString () & "."

My.application.clrversioninuse.minor.tostring () & "." & _

My.application.clrversion.revision.toString () & "." & _

My.application.clrversioninuse.build.tostring ())

MyAppds.myApplication.AddmyApplicationRow ("Command Line", _

My.application.commandline)

DIM CMDS As String

For each cmd as string in my.application.commandlineargs

CMDS & = ", & cmd

NEXT

MyAppds.myApplication.AddmyApplicationRow ("Command Line Args", _

CMDS)

MyAppds.myApplication.AddmyApplicationRow ("Company name", _

My.application.companyname)

MyAppds.myApplication.AddmyApplicationRow ("Company name", _

My.application.companyname)

MyAppds.myApplication.AddmyApplicationRow ("Culture", _

My.application.culture.currentculture.toString ())

MyAppds.myApplication.addmyApplicationRow ("description", _my.application.description

MyAppds.myApplication.AddmyApplicationRow ("Number of Procs", _

My.application.environmentvironmentvariables.Item ("Number_Of_Processors"). Value)

MyAppds.myApplication.AddmyApplicationRow ("File Name", _

My.Application.filename)

MyAppds.myApplication.AddmyApplicationRow ("Folder", _

My.application.folder.toString ())

MyAppds.myApplication.AddmyApplicationRow ("Legal Copyright", _

My.Application.legalcopyright)

MyAppds.myApplication.AddmyApplicationRow ("Legal Trademark", _

My.application.legaltrademark)

MyAppds.myApplication.AddmyApplicationRow ("Event log name", _

My.application.log.eventlogname)

MyAppds.myApplication.AddmyApplicationRow ("Mouse Pointer", _

My.application.mousepointer.tostring)

MyAppds.myApplication.addmyApplicationRow ("Product Name", _

My.application.productname)

MyAppds.myApplication.AddmyApplicationRow ("Cuser Application Data Path", _

My.application.specialfolders.currentuserapplicationData.toString ())

MyAppds.myApplication.addmyApplicationRow ("Title", _

My.application.title)

MyAppds.myApplication.AddmyApplicationRow ("Version", _

My.Application.version.major & "." & My.application.version.minor & "& _

My.Application.version.revision & "." & My.application.version.build)

MyAppds.myApplication.AddmyApplicationRow ("Working Folder", _

My.application.workingfolder.tostring ()

The above code is deliberately written. This code is an attribute that uses the reflection to enumerate the application. Take them in your code to make you see more clear and learn what you can get from a specific property. Each value obtained from the property is placed in a type of data set. Then the My.Applicaton tab is bound to the My.Application data table in this data set. You can get a lot of information about your application by easy access to the properties of the My.Application object. The following table lists the detailed properties of the My.Application object. My.Application

Attribute / method description

CommandLine Returns the full command line - EXE plus all parameters. CommandLineargs Returns a collection of all command line parameters. Companyname Returns the companyName information specified in AssemblyInfo.vb. Culture returns a Culture object that can determine the cultural settings where the current application is located. Description returns the application description information specified in AssemblyInfo.vb. Environment variables lists the environment variables of the current computer. FileName Returns the app name of the application. Folder returns the folder where the application is located. EXIT off the app. LegalCopyRight returns the legal copyright of the application specified in AssemblyInfo.vb. Legal Trademark returns a legal trademark specified in AssemblyInfo.vb. LOG allows you to generate custom file logs or application logs on your local computer. MousePointer allows you to change the mouse pointer of the application. ProductName Returns the application product name specified in AssemblyInfo.vb. SpecialFolders returns a Special Folder object that allows you to access some special folders, such as desktops, Application Data, etc. Title Returns the title version of the executable assembly Returns the version of the assembly WorkingFolder Returns the application's work directory

Related Applications in MY.Application

My.application provides a large number of features, but the code used is much less than using VB6 or VB.NET. Let's take a look at how to use My.Application. For example, we have to write a code to write an event log, now just below this code:

My.Application.log.writentry ("Application Starting", _ EventLoGentrytype.information, 1000)

The above code will write a record in the local application event log. When you used VB.Net, you need to write a lot of line code when you want to write the event log. If you use VB6, you can only use the very limited log feature provided by the App object, you can't specify event IDs, you cannot write to system or security logs, and you cannot create your own custom logs.

Another example can significantly compare the decrease in the amount of code required before and after using My, that is, when you need to generate a custom log and want to write a text file.

My.application.log.FileName = my.application.folder.tostring () & _

"/logfile.txt"

My.application.log.filetype = monitoring.logfiletype.xml

My.Application.log.writentry ("Application Starting", _

EventLoGENTRYTYPE.INFORMATION, 1000)

With these codes, the information to be written is written in addition to the application event log, and is also written to a custom XML log file. You can use My.Application.log's FileMaximumSize property to set the maximum size of the file. You can specify the type of a middle log file, which can be XML, or you can use the TAB segmentation type specified by my.application.log.filety attribute. You can write program controls or different log capabilities, or change the log file at runtime. Figure 2

XML log output

My.Application also allows you to access many application level information directly with a row code. Here are some examples of this:

· Quickly determine the folder where your application is located with the Folder property.

· Quickly set some default cursor types with mousecursor, such as AppStaring, Help, etc.

· Quickly access the original data of some assessments, such as product name, company name, etc.

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

New Post(0)