Using the. NET Framework classes to override API calls (ii)

Source: Internet
Author: User
Tags arrays contains integer tostring

Using the System.Environment class


The System.Environment class provides a number of different information that, if not available, requires multiple Windows API calls. Use System.Environment to retrieve:
    • Information about available drives (GetLogicalDrives method)
    • Number of milliseconds after Windows starts (TickCount property)
    • General environment settings (by CurrentDirectory, MachineName, OSVersion, Systemdirectory, UserDomainName, Userinteractive, UserName, and WorkingSet Property provided)
    • A list of specific folders (provided using the GetFolderPath method)

If you have used the Windows API, you will know that selection methods and properties will replace many API calls, including GetTickCount, GetLogicalDrives, GetSystemDirectory, GetComputerName, GetUserName and GetVersionEx and so on.
In Figure 4 (click on the main form Environment Info[Environment information]] shows the result of executing the GetLogicalDrives method in a list box near the top, including a list of all the specific folders (retrieved using the GetFolderPath method). In the list box at the bottom of the form, the results of many of the properties of the class are displayed.

Figure 4: A form showing the properties and methods used by the System.Environment class
Also, to test the TickCount property, click Test Clickcount(Test Clickcount) to show the results of using the stopwatch class defined in the sample form, and use the following code:
Public Class stopwatch Private Mintstart as Integer public Sub Start () Mintstart = Environment.tickcount E nd Sub public Function Elapsed () as Integer return Environment.tickcount-mintstart end Function public O Verrides Function ToString () as String return String.Format (_ "started: {0}, Elapsed: {1}", _ Mint Start, me.elapsed) End FunctionEnd Class

The Fillproperties method shown here uses a copy of the previously displayed AddItem method to fill the property name and result into the ListView control, as follows:
Private Sub fillproperties () AddItem ("CurrentDirectory", _ environment.currentdirectory)environment.ma Chinename) AddItem ("Osversion.platform", _ Environment.OSVersion.Platform. ToString) AddItem ("Osversion.version", _ Environment.OSVersion.Version).    ToString) AddItem ("Systemdirectory", _ environment.systemdirectory)environment.userdomainname) AddItem ("Userinteractive", _ Environment.userinteractive) AddItem ("UserName", Environment.username) AddItem ("Wo Rkingset ", environment.workingset) End Sub

In addition to using the Environment class, Fillfolderlist actually contains some interesting code. The purpose of this process is to recycle all members of the SpecialFolder enumeration provided by the environment class. (This enumeration contains logical names for physical folders, such as Favorites and history.) This procedure adds the name of each enumeration value to the ListView control on the form, and adds the result of passing the enumeration value to the GetFolderPath method of the Environment object. The procedure shown below can do all of the work:
Private Sub fillfolderlist ()     dim strName as String    dim astrnames () as String    dim Aintvalues as Array    dim i as Integer      ' Use name      ' in SpecialFolder enumeration to fill in the first column of the ListView control.     astrnames = System.Enum.GetNames (_     gettype ( Environment.SpecialFolder))     aintvalues = System.Enum.GetValues (_      gettype (environment.specialfolder))     for i = 0 to astrnames.length-1   & Nbsp;    with LvwFolders.Items.Add (Astrnames (i))              . SubItems.Add (_              Environment.getfolderpath (Aintvalues (i)))         end With     nextend Sub

The example does not hard-code the parameters sent to the environment object, but instead uses the shared GetNames and GetValues methods of the Enum class. By passing the result of the call to the Visual Basic. NET GetType function to GetNames (passing the specified enumeration type), you can retrieve the array that was filled in with the names of all the enumeration members. Repeating the procedure containing the GetValues method returns an Array object that contains all the enumeration values.
Astrnames = System.Enum.GetNames (_ GetType (environment.specialfolder)) Aintvalues = System.Enum.GetValues (_ GetType ( Environment.SpecialFolder))

After these two arrays are given, the remainder of the program is cycled in both arrays, the values in Astrnames are added to ListView, and then the GetFolderPath method of the Environment class is invoked to retrieve the appropriate path:
For i = 0 to astrnames.length-1 with LVWFOLDERS.ITEMS.ADD (Astrnames (i)). SubItems.Add (_ Environment.getfolderpath (Aintvalues (i))) End WithNext

The upper ListView control in Figure 4 contains the output of the code.
Tip:The Enum gives you some tips that are not available in Visual Basic 6.0, such as the GetNames and GetValues methods shown in the example. For more information about using the features of the Enum class, see the. NET Framework documentation.

Using the Windows.Forms.SystemInformation class


When you provide a well-designed user interface, you often need to determine the current Windows settings, such as the height and width of the icon, or the width of the scroll bar. In Visual Basic 6.0, you can use the GetSystemMetrics and SystemParametersInfo Windows API functions to determine many of these settings. In the. NET framework, you can use the SystemInformation class provided by the Windows.Forms namespace.
Although the code used by Frmsysteminformation in Figure 5 is not very interesting, it does show all the properties that the class provides. (On the main form, click systeminformation InfoSystem Information can test this sample form. The sample form operates on all the properties of the SystemInformation class, displaying the name and current value of each property in the form's ListView control.

Figure 5: Sample form showing all properties of the Windows.Forms.SystemInformation class
The sample form uses the AddItem method described earlier to simply loop and display output in all 60 properties of the SystemInformation class:
AddItem ("ArrangeDirection", _ Systeminformation.arrangedirection) AddItem ("Startingposition", _ systeminformation.arrangestartingposition) AddItem ("Bootmode", systeminformation.bootmode) ' et cetera ...
Summary
    • Although you can use platform invoke services (P/invoke) in Visual Basic. NET to handle unmanaged code and then call the Windows API directly, you should still look for alternatives when creating Visual Basic. NET Applications. However, you don't have to worry about p/invoke details, because the Declare statement will deal with these details for you.
    • The. NET Framework does not encapsulate all the Windows API features, but actions you might want to use with API calls in Visual Basic 6.0 can also be implemented in the. NET Framework.
    • The Registry and RegistryKey classes simplify the operation of the Windows registry, helping developers avoid a large number of API calls.
    • The FileDialog, ColorDialog, FontDialog, and Printerdialog classes make it easy for you to use common dialog boxes in Windows. You do not need to call the Windows APIs directly, nor do you need to use the dreaded CommonDialog ActiveX control.
    • You can use the FileVersionInfo class to retrieve all file version information built into executable files, driver files, and DLL files. This class can replace some of the complex Windows API code that is required in Visual Basic 6.0.
    • The environment and SystemInformation classes allow you to easily retrieve system settings, and if they are not, you need to invoke many different API functions.

About the author


Ken Getz is a senior advisor to MCW Technologies, whose job involves programming, writing and training. He is proficient in tools and applications written in Microsoft Access, Visual Basic, and Office and BackOffice suites. He has also written many books with others, including: "Access Developer's Handbook", co-authored by Paul Litwin and Mike Gilbert, and the Ac from Paul Litwin and Mike Gilbert Cess Developer's Handbooks, "Access Gunderloy 2002 's Developer", co-authored by Paul Litwin and Mike Handbooks, and Mike Gilbert, co-author of " Visual Basic Language Developer ' s Handbook, and the VBA Sybex ' Developer, co-authored by Mike Gilbert (Handbook). He also participated in the preparation of the AppDev training materials and engaged in the teaching work in this field. Ken has often spoken at the technical meeting, and since 1994 he has delivered speeches at every Microsoft tech*ed meeting. Ken is a technical editor for Access/vb/sql Advisor and an electronic writer for the Microsoft Office Solutions magazine under informant communication Group.

About Informant Communications Group


Informant Communications Group, Inc. (www.informant.com) is a multimedia company focused on the information technology industry. Founded in 1990, ICG is dedicated to the fields of publications, conferences, catalogues, and WEB sites related to software development. With offices in the United States and the UK, ICG has become a prestigious media and marketing content integrator and has been meeting the growing needs of IT personnel with high quality technical information.
©2002 informant Communications Group and Microsoft Corporation are all copyrighted.
Technical Editor: KnG Consulting

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.