Introduce my namespace to Visual Basic programmers (i)

Source: Internet
Author: User
Tags command line contains functions log object model tostring web services win32
visual| Program | programmer | Namespace excerpted from "Microsoft Visual Basic Programmer ' s Introduction to Whidbey" by Sean Campbell, Scott Sw Igart, 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 using VB6, you used VB's Run-time library, which contains a large number of COM objects, and the Windows version of the Win32 API. The original version of the. NET Framework integrates much of this into a large class library, which is our familiar base class libraries (BCL). Some classes in BCL support access to the bottom of the system, some classes give you easy access to information about your computer's hardware, and some classes allow you to communicate across different applications, encrypt data, access the registry, and more. If you want to make a thorough. NET developer, you have to master BCL and the richness of the functionality it offers. If you don't know enough about BCL, developers will do a lot of unnecessary repetitive work and reconstruct some existing classes. Sometimes. NET provides a feature that is "hidden" too deep, or is too obscure relative to the large. NET Framework. There are a lot of people who write articles about functions that are implemented with their ancestral algorithms that can be easily implemented with certain classes in the framework, such as path or Passwordderivebyes. In fact, many people will tell you how to learn. NET is not actually learning Visual Basic or C #, but learning BCL. But learning BCL is also a huge challenge, because it's too big. Some of the most commonly used classes are mixed with classes that are almost impossible to use. To make it easier to find the classes that are most commonly used in those frameworks, Visual Basic 2005 now provides a "shortcut" namespace--my. You can easily access computers, applications, and user information by using my, and you can use it to access forms and Web services. The point to note is that I can only use Visual Basic 2005 for development and cannot access my directly when using C #. It is important to mention the My namespace and not just a shortcut. In some cases, my namespace provides an analogy that you can easily search for in a large number of namespaces to get a more powerful class of BCL. For example, the Folder object provides more properties, such as drive, which do not exist in the System.IO.DirectoryInfo class at all. My also lets Microsoft think about "Computer", "Application", or "user" as a complete entity, and ask questions about "what you should be able to do with a computer". The result of thinking includes My.Computer.Network.Ping, which allows you to use a line of code to Ping a network computer. The feature you're using is only called a COM library before, even the Win32 API, and now it's easy to find and use in my.

The My namespace is divided into several zones below:

My object model

Object description

My.Application
Contains information about the currently running application, such as the title, current path, and version. It also accesses environment variables, allowing you to easily write local application logs or custom logs.

My.Computer
Provides information about the local computer hardware or the underlying system on which the application is running. such as the registry, printers, keyboards, reality and so on. These are the objects that you can access through this class.

My.User
Contains information about the current user, including the user display name, domain name, group name, and so on.

My.WebServices
It allows you to easily access Web services that have already added Web references to your project.

My.Forms
A collection of forms that exist in the current project. You can show and hide these forms through it without having to explicitly create their objects in your code. (This is great, VB6 the Gospel of programmers)


As you can see, my namespace gives you a convenient way to access those common features directly in your daily development.

Deep code

In this section, I'll take you through a short example to get a deeper understanding of my namespace. This example explores the world within me in a certain depth. This program has many tabs, each of which focuses on one of my major features.



Figure 1

My.Application tab.

Image modification-Need to delete my.datasources– This part has been removed.
My.Application
The first tab--my.application has a data table control that displays most of the property values for the My.Application object. The code to control this data table control is as follows:

Clrversioninuse has been removed (you can see it in M2 bits, but it has been removed)

MyAppDS.MyApplication.AddMyApplicationRow ("CLR Version in Use", _

My.Application.CLRVersionInUse.Major.ToString () & "." & _

My.Application.CLRVersionInUse.Minor.ToString () & "." & _

My.Application.CLRVersionInUse.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.EnvironmentVariables.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 in a bit verbose. This code is enumerating the properties of the application using reflection. Put them all in the code to make you see more clearly and understand what you can get from a particular attribute. Each value obtained from the attribute is placed in a typed dataset. A data table control in the My.applicaton tab is then bound to the My.Application datasheet in the dataset. By easily accessing the properties of the My.Application object, you can get a lot of information about your application. 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 that contains all command-line arguments.

CompanyName
Returns the COMPANYNAME information specified in the AssemblyInfo.vb.

Culture
Returns a culture object through which you can determine the cultural settings of the current application.

Description
Returns the application description information specified in AssemblyInfo.vb.

Environment Variables
Enumerates the environment variables for the current computer.

FileName
Returns the assembly name of the application.

Folder
Returns the folder where the application resides.

Exit
Closes the application.

Legalcopyright
Returns the legal copyright of the application specified in AssemblyInfo.vb.

Legal Trademark
Returns the legal trademark for the application 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 application's mouse pointer.

ProductName
Returns the name of the application product specified in AssemblyInfo.vb.

Specialfolders
Returns a special folder object that lets you access some special folders, such as desktop, application data, and so on.

Title
Returns the title of an executable assembly

Version
Returns the version information for an assembly

WorkingFolder
Returns the working directory of the application


The related application of my.application in development

My.Application provides a lot of functionality, but uses much less code than VB6 or vb.net. Let's see how to use My.Application. For example, we write a piece of code to write the event log, and now as long as the following code is enough:

My.Application.Log.WriteEntry ("Application Starting", _
Eventlogentrytype.information, 1000)

The above code writes a record in the local application event log. If you want to write to an event log, you need to write a lot of lines of code before using vb.net. With VB6, you can only use the very limited log functionality provided by the App object, you cannot specify an event ID, write to the system or security log, or create a custom log.

There is also an example where you can clearly compare the decrease in the amount of code needed before and after using my, which 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.WriteEntry ("Application Starting", _

Eventlogentrytype.information, 1000)

With this code, the information to be written is accidentally written into a custom XML log file in addition to being written into the application event log. You can use the My.Application.Log Filemaximumsize property to set the maximum size of the file. You can specify the type of log file, either XML or the tab split type specified with the My.Application.Log.FileType attribute. You can write program control using or different log functions, and you can change the log file at run time.



Figure 2 Figure II

XML Log Output

My.Application also allows you to access many application-level information directly in one line of code. Here are some examples of this:

· Use the Folder property to quickly determine where your application resides.

· Use Mousecursor to quickly set some of the default cursor types, such as Appstaring,,help

· Quick access to the original data of some assemblies, such as product name, company name, and so on.

< to Be continued >



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.