Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - WMI Programming with Visual Basic.NET: Man...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

WMI Programming with Visual Basic.NET: Managing the OS, continued
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-07-12

    Table of Contents:
  • WMI Programming with Visual Basic.NET: Managing the OS, continued
  • Retrieving the list of all services
  • How to know the state of a service
  • How to modify (manage) the state of a service
  • How the above program works
  • How will I know the proper description of the error?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    WMI Programming with Visual Basic.NET: Managing the OS, continued - How to know the state of a service


    (Page 3 of 6 )

    Every service generally starts when the OS starts and stops automatically when the OS shuts down (unless the respective service is configured differently). But we can also pause, resume or stop the service manually (based on the user's credentials and type of service).  Now the question is, how can you know the state of a service, whether running or stopped or paused or resumed or what?

    There exists a property called “state” within the “Win32_Service” class. This holds the current state of the service object selected. The following code fragment show you how to fetch that property:

    Me.lblState.Text = ""

           Me.lblState.Refresh()

     

           DimserviceNameAsString=Me.lstServices.SelectedItem.
    ToString

           DimsearcherAsNewManagementObjectSearcher("SELECT * FROM Win32_service WHERE DisplayName='" & serviceName & "'")

           DimoItemsAsManagementObjectCollection = searcher.Get()

           Dimar(0)AsObject

            oItems.CopyTo(ar, 0)

           DimoitemAsManagementObject = ar(0)

           Me.lblState.Text = oitem("State")

    The first two lines will actually clear the previous message.  If you carefully observe the above program, you'll see that I used the SELECT statement of WQL (explained in part two of my series) along with a WHERE clause based on the service name selected within the listbox.  So, it is confirmed that we will get only one object of “Win32_service” (unless more services exist with the same name). 

    Since there exists only one service with that name (assumed), I created an array with only one element (zero yields to single location) and copying the service object to the array (only one gets copied).  After that, I assign that array element to “oItem” of type “ManagementObject”, where we can get the state of the service.  Consider my apologies, as I could not find a better solution than this method of retrieving a single object from “ManagementObjectCollection”.  I would be very glad if anyone could investigate and give me a better solution. 

    And finally I am assigning the current status of “state” to the label.  The following figure (Fig 2) shows you the status of “Indexing Service” (at the bottom of the window) selected within the listbox (on my system).

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


     

    VISUAL BASIC.NET ARTICLES

    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway