ASP.NET Monitor Windows Services

Today I had to troubleshoot a mail problem on a web server. I needed to determine if the SMTP Service was running. I used to have an ASP script to list Windows Services and their status but I needed something written in ASP.NET. As usual, I had problems finding any sample code on the Internet and there was a technical issue that frustrated me. The technical problem is that you need to manually add a reference to System.ServiceProcess in Visual Studio.NET 2003. You cannot use an Imports statement to reference that Namespace.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        ' Define a DataSet with a single DataTable.
        ds.Clear()
        ds.Tables.Add("Services")

        ' Define two columns for this table.
        ds.Tables("Services").Columns.Add("service_name")
        ds.Tables("Services").Columns.Add("service_status")

        Try
            Dim services() As System.ServiceProcess.ServiceController
            services = System.ServiceProcess.ServiceController.GetServices()
            Dim iCounter As Integer

            For iCounter = 0 To services.Length - 1
                rs = ds.Tables("Services").NewRow()
                rs("service_name") = services(iCounter).DisplayName
                rs("service_status") = services(iCounter).Status
                ds.Tables("Services").Rows.Add(rs)
            Next

            rptServices.DataSource = ds
            rptServices.DataBind()
        Catch ex As Exception
            lblError.Text = ex.Message & "<br>" & ex.StackTrace
        End Try
    End Sub
This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.