I am developing a small little Service Control Application. I am
using a listview control with checkboxes and getting the list of
services I want to control from a text file. When you check a
checkbox, it should either stop the service or start the service
depending on each respective service's status. I want the listView
to update the status for each service as they are checked. instead
all I can seem to do is clear the listView and I try running the
ListServices Function I created again, but the application just
hangs. The code is below:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.ServiceProcess;
using System.IO;
using System.Diagnostics;
namespace AutoRestart_Services
{
/// <summary>
/// Summary description for scMainForm.
/// </summary>
public class scMainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader ServiceName;
private System.Windows.Forms.ColumnHeader ServiceStatus;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public scMainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.listView1 = new System.Windows.Forms.ListView();
this.ServiceName = new
System.Windows.Forms.ColumnHeader();
this.ServiceStatus = new
System.Windows.Forms.ColumnHeader();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.fileMenu});
this.menuItem1.Text = "File";
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.Text = "Close";
this.fileMenu.Click += new
System.EventHandler(this.fileMenu_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.menuItem3});
this.menuItem2.Text = "Configuration";
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "";
//
// listView1
//
this.listView1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.listView1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.ServiceName,
this.ServiceStatus});
this.listView1.GridLines = true;
this.listView1.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.Nonclickabl e;
this.listView1.Location = new System.Drawing.Point(16,
32);
this.listView1.Name = "listView1";
this.listView1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.listView1.Size = new System.Drawing.Size(392, 168);
this.listView1.TabIndex = 2;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.li stView1_ItemCheck);
//
// ServiceName
//
this.ServiceName.Text = "Service Name";
this.ServiceName.Width = 275;
//
// ServiceStatus
//
this.ServiceStatus.Text = "Service Status";
this.ServiceStatus.TextAlign =
System.Windows.Forms.HorizontalAlignment.Right;
this.ServiceStatus.Width = 90;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Highlight;
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.label1.ForeColor =
System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(392, 16);
this.label1.TabIndex = 5;
this.label1.Text = "Windows Services";
//
// scMainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(426, 219);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {
this.label1,
this.listView1});
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Menu = this.mainMenu1;
this.Name = "scMainForm";
this.Text = "Windows Service Controller";
this.Load += new
System.EventHandler(this.scMainForm_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new scMainForm());
}
private void scMainForm_Load(object sender, System.EventArgs
e)
{
ListServices();
}
private void ListServices()
{
//Open text file and read each line to determine what services we
want to manipulate
StreamReader sr;
string line;
sr=File.OpenText("ntservices.txt");
line=sr.ReadLine();
//Add each line into an array for later
ArrayList lines = new ArrayList();
//Loop through text file lines and add to array
while(line!=null)
{
lines.Add(line);
line=sr.ReadLine();
}
sr.Close();
//Create ServiceController to administer a Windows Service
ServiceController sc = new ServiceController();
//Run the autoRestartServices function and pass each line from the
text file to
//it, so that each service will be administered.
for(int x = 0; x < lines.Count; x++)
{
sc.ServiceName = lines[x].ToString();
listView1.Items.Add(sc.DisplayName);
listView1.Items[x].SubItems.Add(sc.Status.ToString());
}
listView1.CheckBoxes = true;
}
private void fileMenu_Click(object sender, System.EventArgs
e)
{
scMainForm.ActiveForm.Close();
}
private void listView1_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{
if (e.CurrentValue==CheckState.Unchecked)
{
autoRestartServices(listView1.Items[e.Index].Text);
listView1.Update();
}
else
{
autoRestartServices(listView1.Items[e.Index].Text);
listView1.Update();
}
}
private void autoRestartServices(string serviceDisplayName)
{
//Create ServiceController to administer a Windows Service
ServiceController sc = new ServiceController();
sc.DisplayName = serviceDisplayName;
//STOP THE WINDOWS SERVICE
if(sc.Status.ToString() == "Running")
{
//Stop the service
sc.Stop();
//Wait for status of service to change to stopped
sc.WaitForStatus(ServiceControllerStatus.Stopped);
}
//RESTART THE WINDOWS SERVICE
if(sc.Status.ToString() == "Stopped")
{
//Restart the service
sc.Start();
//Wait for status of service to change to running
sc.WaitForStatus(ServiceControllerStatus.Running);
}
}
}
}
All help is appreciated. . . .
Thanks,
willow1480