C# Convert.. critique my code!!!
Shorty
Manchester, UK Icrontian
ok ok, I admit it.
I HATED .NET. I DID, I DO still for web applications. .NET is a framework for web apps.. but... for desktop compiled apps... I've become a convert
My new employer asked me if I could write "something" that could stop and restart a remote service on a list of servers in a domain....
So I crashed coursed C# this weekend
Any gurus wanna help me make it more efficent??
I HATED .NET. I DID, I DO still for web applications. .NET is a framework for web apps.. but... for desktop compiled apps... I've become a convert

My new employer asked me if I could write "something" that could stop and restart a remote service on a list of servers in a domain....
So I crashed coursed C# this weekend

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.ServiceProcess;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox BrowseField;
private System.Windows.Forms.Button BrowseButton;
private System.Windows.Forms.Button RestartServices;
private System.ServiceProcess.ServiceController serviceController1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button ExportButton;
private System.Windows.Forms.RichTextBox outputBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.groupBox1 = new System.Windows.Forms.GroupBox();
this.RestartServices = new System.Windows.Forms.Button();
this.BrowseButton = new System.Windows.Forms.Button();
this.BrowseField = new System.Windows.Forms.TextBox();
this.serviceController1 = new System.ServiceProcess.ServiceController();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.ExportButton = new System.Windows.Forms.Button();
this.outputBox = new System.Windows.Forms.RichTextBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.RestartServices);
this.groupBox1.Controls.Add(this.BrowseButton);
this.groupBox1.Controls.Add(this.BrowseField);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(680, 112);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Red Alert ";
//
// RestartServices
//
this.RestartServices.Location = new System.Drawing.Point(192, 72);
this.RestartServices.Name = "RestartServices";
this.RestartServices.Size = new System.Drawing.Size(304, 24);
this.RestartServices.TabIndex = 2;
this.RestartServices.Text = "Restart Red Alert Services";
this.RestartServices.Click += new System.EventHandler(this.RestartServices_Click);
//
// BrowseButton
//
this.BrowseButton.Location = new System.Drawing.Point(32, 24);
this.BrowseButton.Name = "BrowseButton";
this.BrowseButton.Size = new System.Drawing.Size(264, 32);
this.BrowseButton.TabIndex = 1;
this.BrowseButton.Text = "Browse for Server Listing Text File";
this.BrowseButton.Click += new System.EventHandler(this.BrowseButton_Click);
//
// BrowseField
//
this.BrowseField.Location = new System.Drawing.Point(312, 32);
this.BrowseField.Name = "BrowseField";
this.BrowseField.Size = new System.Drawing.Size(352, 20);
this.BrowseField.TabIndex = 0;
this.BrowseField.Text = "";
this.BrowseField.TextChanged += new System.EventHandler(this.BrowseField_TextChanged);
//
// serviceController1
//
this.serviceController1.MachineName = "\'";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(16, 144);
this.progressBar1.Maximum = 200;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(680, 23);
this.progressBar1.TabIndex = 2;
//
// ExportButton
//
this.ExportButton.Location = new System.Drawing.Point(576, 528);
this.ExportButton.Name = "ExportButton";
this.ExportButton.Size = new System.Drawing.Size(112, 23);
this.ExportButton.TabIndex = 3;
this.ExportButton.Text = "Export To Text File";
this.ExportButton.Visible = false;
this.ExportButton.Click += new System.EventHandler(this.ExportButton_Click);
//
// outputBox
//
this.outputBox.Location = new System.Drawing.Point(16, 176);
this.outputBox.Name = "outputBox";
this.outputBox.ShowSelectionMargin = true;
this.outputBox.Size = new System.Drawing.Size(680, 344);
this.outputBox.TabIndex = 4;
this.outputBox.Text = "";
this.outputBox.Visible = true;
this.outputBox.TextChanged += new System.EventHandler(this.outputBox_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(720, 565);
this.Controls.Add(this.outputBox);
this.Controls.Add(this.ExportButton);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Red Alert Network Stop/Start";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/*private void folderBrowserDialog1_HelpRequest(object sender, System.EventArgs e)
{
}*/
private void BrowseButton_Click(object sender, System.EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Hyena / New Line Delimited Text File" ;
fdlg.InitialDirectory = @"c:\" ;
fdlg.Filter = "All files (*.*)|*.*|txt (*.txt)|*.txt" ;
fdlg.FilterIndex = 2 ;
fdlg.RestoreDirectory = true ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
BrowseField.Text = fdlg.FileName;
}
}
private void BrowseField_TextChanged(object sender, System.EventArgs e)
{
}
private void RestartServices_Click(object sender, System.EventArgs e)
{
if(BrowseField.Text == "")
{
MessageBox.Show("No Text File Selected!");
}
else
{
RestartServices.Enabled = false;
BrowseButton.Enabled = false;
BrowseField.Enabled = false;
if(!ReadFileForIndexing())
{
badFile();
ClearFields();
}
else
{
DateTime tn = DateTime.Now;
outputBox.AppendText(tn.ToString()+ ": Processing " + BrowseField.Text + " \n");
Process();
outputBox.AppendText(" \n");
outputBox.AppendText("Job Complete - Review Log File for further information" + " \n");
outputBox.Visible = true;
ExportButton.Visible = true;
}
}
}
private bool ReadFileForIndexing()
{
string path = BrowseField.Text;
// Open the file to read from.
try
{
StreamReader sr = File.OpenText(path);
}
catch(IOException)
{
return false;
}
return true;
}
private void Process()
{
string s = "";
string path = BrowseField.Text;
StreamReader srReal = File.OpenText(path);
System.Collections.ArrayList al = new ArrayList();
int i = 0;
while ((s = srReal.ReadLine()) != null)
{
al.Add((string) s);
i++;
}
srReal.Close();
progressBar1.Maximum = i;
//restartRA(NewStr);
//This will go through each list but for now, lets just use \\imperium!
foreach(string NewStr in al)
{
DateTime tn = DateTime.Now;
outputBox.AppendText(" \n");
outputBox.AppendText(tn.ToString()+ ": Server - " + NewStr + " \n");
restartRA(NewStr);
progressBar1.Increment(1);
}
}
public void restartRA(string str)
{
System.ServiceProcess.ServiceController service =
new System.ServiceProcess.ServiceController("Telnet", str);
try
{
if (service.Status.Equals(ServiceControllerStatus.Stopped))
{
// Start the service if the current status is stopped.
DateTime tn = DateTime.Now;
outputBox.AppendText(tn.ToString()+ ": Red Alert has already stopped! - Restarting... \n");
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
else if(service.Status.Equals(ServiceControllerStatus.StopPending))
{
DateTime tn = DateTime.Now;
outputBox.AppendText(tn.ToString()+ ": Red Alert is already stopping - Waiting... \n");
service.WaitForStatus(ServiceControllerStatus.Stopped);
DateTime tp = DateTime.Now;
outputBox.AppendText(tp.ToString()+ ": Restarting Red Alert \n");
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
else
{
// Stop the service if its status is not set to "Stopped".
DateTime tn = DateTime.Now;
outputBox.AppendText(tn.ToString()+ ": Stopping Red Alert \n");
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
DateTime tp = DateTime.Now;
outputBox.AppendText(tp.ToString()+ ": Restarting Red Alert \n");
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
// Refresh and display the current service status.
//sc.Refresh();
}
catch(System.InvalidOperationException)
{
DateTime tn = DateTime.Now;
outputBox.AppendText(tn.ToString()+ ": Red Alert Service does not exist on machine:: " + str + " ... will ignore... \n");
}
}
private void ClearFields()
{
RestartServices.Enabled = true;
BrowseButton.Enabled = true;
BrowseField.Enabled = true;
BrowseField.Text = "";
}
private void badFile()
{
outputBox.AppendText("Error Opening! " + BrowseField.Text + " \n");
MessageBox.Show(
"This file is open for writing. " +
"Please save and close before selecting again.");
}
private void StartLog()
{
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void ExportButton_Click(object sender, System.EventArgs e)
{
string toSave = outputBox.ToString();
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = "log.txt";
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
sfd.FilterIndex = 2 ;
sfd.RestoreDirectory = true ;
Stream myStream;
if(sfd.ShowDialog() == DialogResult.OK)
{
if((myStream = sfd.OpenFile()) != null)
{
StreamWriter wText =new StreamWriter(myStream);
wText.Write(toSave);
myStream.Close();
}
}
}
private void outputBox_TextChanged(object sender, System.EventArgs e)
{
}
}
}
Any gurus wanna help me make it more efficent??
0