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.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.ServiceP rocess;
using System.IO;
using System.Diagnost ics;
namespace AutoRestart_Ser vices
{
/// <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.ColumnHea der ServiceName;
private System.Windows. Forms.ColumnHea der 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.Componen tModel.Containe r components = null;
public scMainForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent
call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
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.ServiceNam e = new
System.Windows. Forms.ColumnHea der();
this.ServiceSta tus = new
System.Windows. Forms.ColumnHea der();
this.label1 = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// mainMenu1
//
this.mainMenu1. MenuItems.AddRa nge(new
System.Windows. Forms.MenuItem[] {
this.menuItem1,
this.menuItem2} );
//
// menuItem1
//
this.menuItem1. Index = 0;
this.menuItem1. MenuItems.AddRa nge(new
System.Windows. Forms.MenuItem[] {
this.fileMenu}) ;
this.menuItem1. Text = "File";
//
// fileMenu
//
this.fileMenu.I ndex = 0;
this.fileMenu.T ext = "Close";
this.fileMenu.C lick += new
System.EventHan dler(this.fileM enu_Click);
//
// menuItem2
//
this.menuItem2. Index = 1;
this.menuItem2. MenuItems.AddRa nge(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.BorderSty le.FixedSingle;
this.listView1. Columns.AddRang e(new
System.Windows. Forms.ColumnHea der[] {
this.ServiceNam e,
this.ServiceSta tus});
this.listView1. GridLines = true;
this.listView1. HeaderStyle =
System.Windows. Forms.ColumnHea derStyle.Noncli ckable;
this.listView1. Location = new System.Drawing. Point(16,
32);
this.listView1. Name = "listView1" ;
this.listView1. RightToLeft = System.Windows. Forms.RightToLe ft.Yes;
this.listView1. Size = new System.Drawing. Size(392, 168);
this.listView1. TabIndex = 2;
this.listView1. View = System.Windows. Forms.View.Deta ils;
this.listView1. ItemCheck += new
System.Windows. Forms.ItemCheck EventHandler(th is.listView1_It emCheck);
//
// ServiceName
//
this.ServiceNam e.Text = "Service Name";
this.ServiceNam e.Width = 275;
//
// ServiceStatus
//
this.ServiceSta tus.Text = "Service Status";
this.ServiceSta tus.TextAlign =
System.Windows. Forms.Horizonta lAlignment.Righ t;
this.ServiceSta tus.Width = 90;
//
// label1
//
this.label1.Bac kColor = System.Drawing. SystemColors.Hi ghlight;
this.label1.Fla tStyle = System.Windows. Forms.FlatStyle .Popup;
this.label1.Fon t = new System.Drawing. Font("Microsoft Sans
Serif", 9.75F, System.Drawing. FontStyle.Regul ar,
System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.label1.For eColor =
System.Drawing. SystemColors.Ac tiveCaptionText ;
this.label1.Loc ation = new System.Drawing. Point(16, 16);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(392, 16);
this.label1.Tab Index = 5;
this.label1.Tex t = "Windows Services";
//
// scMainForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.BackColor = System.Drawing. SystemColors.De sktop;
this.ClientSize = new System.Drawing. Size(426, 219);
this.Controls.A ddRange(new
System.Windows. Forms.Control[] {
this.label1,
this.listView1} );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Menu = this.mainMenu1;
this.Name = "scMainForm ";
this.Text = "Windows Service Controller";
this.Load += new
System.EventHan dler(this.scMai nForm_Load);
this.ResumeLayo ut(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.EventArg s
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.OpenTex t("ntservices.t xt");
line=sr.ReadLin e();
//Add each line into an array for later
ArrayList lines = new ArrayList();
//Loop through text file lines and add to array
while(line!=nul l)
{
lines.Add(line) ;
line=sr.ReadLin e();
}
sr.Close();
//Create ServiceControll er to administer a Windows Service
ServiceControll er sc = new ServiceControll er();
//Run the autoRestartServ ices 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.Display Name);
listView1.Items[x].SubItems.Add(s c.Status.ToStri ng());
}
listView1.Check Boxes = true;
}
private void fileMenu_Click( object sender, System.EventArg s
e)
{
scMainForm.Acti veForm.Close();
}
private void listView1_ItemC heck(object sender,
System.Windows. Forms.ItemCheck EventArgs e)
{
if (e.CurrentValue ==CheckState.Un checked)
{
autoRestartServ ices(listView1. Items[e.Index].Text);
listView1.Updat e();
}
else
{
autoRestartServ ices(listView1. Items[e.Index].Text);
listView1.Updat e();
}
}
private void autoRestartServ ices(string serviceDisplayN ame)
{
//Create ServiceControll er to administer a Windows Service
ServiceControll er sc = new ServiceControll er();
sc.DisplayName = serviceDisplayN ame;
//STOP THE WINDOWS SERVICE
if(sc.Status.To String() == "Running")
{
//Stop the service
sc.Stop();
//Wait for status of service to change to stopped
sc.WaitForStatu s(ServiceContro llerStatus.Stop ped);
}
//RESTART THE WINDOWS SERVICE
if(sc.Status.To String() == "Stopped")
{
//Restart the service
sc.Start();
//Wait for status of service to change to running
sc.WaitForStatu s(ServiceContro llerStatus.Runn ing);
}
}
}
}
All help is appreciated. . . .
Thanks,
willow1480