Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 7
    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


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


    (Page 4 of 6 )

    This section actually manipulates (manages) the state of the service selected from the listbox. The “Win32_service” class mainly has the following methods to change the state of the service:

    • StartService

    • StopService

    • PauseService

    • ResumeService

    Of course, there exist a few more too, but we shall concentrate on only the first two methods. The implementations of the last two methods are very similar to those of the first two methods. Recalling from the first section, we need to work with “InvokeMethod” of the class “ManagementObject” and receive the results in the form of an object of type “ManagementBaseObject” class. The following is the code fragment which tries to “stop” the service selected from the listbox:

    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)

     

           ' Execute the method

           DimoutParamsAsManagementBaseObject = oitem.InvokeMethod("StopService",Nothing,Nothing)

           DimobjAsObject= outParams("ReturnValue")

           Ifobj.ToString = "0"Then

                MessageBox.Show("Succesfully stopped..")

                lstServices_SelectedIndexChanged(Nothing,Nothing)

           Else

                MessageBox.Show("Could not Stop, Returned code:" & obj.ToString)

           EndIf

    The above fragment stops the service. To start the service, just replace “StopService” in the above program with “StartService” and modify the messages being displayed using “MessageBox.Show” statements. You could see a message box showing “Succesfully stopped” as shown in the following figure (Fig 3).

    The next section gives a detailed explanation about the previous code fragment.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


     

    VISUAL BASIC.NET ARTICLES

    - Clocks and Countdowns
    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek