Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ServiceController.ServiceName Property

Gets or sets the name that identifies the service that this instance references.

[Visual Basic]
Public Property ServiceName As String
[C#]
public string ServiceName {get; set;}
[C++]
public: __property String* get_ServiceName();
public: __property void set_ServiceName(String*);
[JScript]
public function get ServiceName() : String;
public function set ServiceName(String);

Property Value

The name that identifies the service that this ServiceController instance references. The default is an empty string ("").

Exceptions

Exception Type Condition
ArgumentNullException The ServiceName is a null reference (Nothing in Visual Basic).
ArgumentException The syntax of the ServiceName property is invalid.

Remarks

The ServiceName identifies the service to the Service Control Manager. Changing this property causes the ServiceController instance to bind to another service, it does not change what the Service Control Manager's Microsoft Management Console snapin displays.

When you are implementing a custom service, the value of this property must be identical to the name recorded for the service in the ServiceName property of the corresponding ServiceInstaller class. In code, the ServiceName is usually set in the main() function of the executable.

When you reset the ServiceName property, the method that sets the property sets this instance's DisplayName to an empty string ("").

Example

[Visual Basic, C#, C++] The following example uses the ServiceController class to check whether the Alerter service is stopped. If the service is stopped, the example starts the service and waits until the service status is set to Running.

[Visual Basic] 

         ' Check whether the Alerter service is started.

         Dim sc As New ServiceController()
         sc.ServiceName = "Alerter"
         Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)
         
         If sc.Status = ServiceControllerStatus.Stopped Then
            ' Start the service if the current status is stopped.
            Console.WriteLine("Starting the Alerter service...")

            Try
               ' Start the service, and wait until its status is "Running".
               sc.Start()
               sc.WaitForStatus(ServiceControllerStatus.Running)
               
               ' Display the current service status.
               Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status)
            Catch 
               Console.WriteLine("Could not start the Alerter service.")
            End Try
         End If


[C#] 

         // Check whether the Alerter service is started.

         ServiceController sc  = new ServiceController();
         sc.ServiceName = "Alerter";
         Console.WriteLine("The Alerter service status is currently set to {0}", 
                            sc.Status.ToString());

         if (sc.Status == ServiceControllerStatus.Stopped)
         {
            // Start the service if the current status is stopped.

            Console.WriteLine("Starting the Alerter service...");
            try 
            {
               // Start the service, and wait until its status is "Running".
               sc.Start();
               sc.WaitForStatus(ServiceControllerStatus.Running);
            
               // Display the current service status.
               Console.WriteLine("The Alerter service status is now set to {0}.", 
                                  sc.Status.ToString());
            }
            catch (InvalidOperationException)
            {
               Console.WriteLine("Could not start the Alerter service.");
            }
         }

[C++] 

         // Check whether the Alerter service is started.

         ServiceController *sc  = new ServiceController();
         if (sc)
         {
            sc->ServiceName = "Alerter";
            Console::WriteLine("The Alerter service status is currently set to {0}", 
               __box(sc->Status));

            if (sc->Status == (ServiceControllerStatus::Stopped))
            {
               // Start the service if the current status is stopped.

               Console::WriteLine("Starting the Alerter service...");
               try 
               {
                  // Start the service, and wait until its status is "Running".
                  sc->Start();
                  sc->WaitForStatus(ServiceControllerStatus::Running);
            
                  // Display the current service status.
                  Console::WriteLine("The Alerter service status is now set to {0}.", 
                     __box(sc->Status));
               }
               catch (InvalidOperationException *e)
               {
                  Console::WriteLine("Could not start the Alerter service.");
               }
            }
         }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

ServiceController Class | ServiceController Members | System.ServiceProcess Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.