G
Guest
Hi
I have a Windows Form that I have 3 textboxes and some buttons. Below is
the code that I have implemented it reads a pile of files from a folder and
then reads each of the files. If the McAfee Dat file version is old (older
than 15 revisions) then it will output the machine name, Scan Engine, and dat
file version (all of this is each of the text files that I load)
Is the way that I coded it poor C# OOP? I mean I have a registry class that
I have borrowed and implemented "using" so that I have access to the class.
Is it a bad practice to have so much code after a button like I do in my Run
button??
BTW I am using C# Express 2005 Beta 1....
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Utility.ModifyRegistry; // Class borrowed from the Internet
using System.IO;
#endregion
namespace McAfeeVersionCheck
{
partial class MainForm : Form
{
// Create an instance of the the registry class. Needs to be here
// because of the Save button (which saves the stuff to Registry)
(SCOPE)
ModifyRegistry myRegistry = new ModifyRegistry();
public MainForm()
{
InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit(); // Close Program
}
private void MainForm_Load(object sender, EventArgs e)
{
// Create an instance of the class.
//ModifyRegistry myRegistry = new ModifyRegistry();
// Creates strings and stores a value for each
// Registry reads from HKLM\SOFTWARE\<ProgramName>
string strMcAfeeVersion = myRegistry.Read("McAfeeVersion");
string strScanEngine = myRegistry.Read("ScanEngine");
string strDatFile = myRegistry.Read("DatFile");
if ((strMcAfeeVersion != null && strScanEngine != null &&
strDatFile != null) && (strMcAfeeVersion != "" || strScanEngine != "" ||
strDatFile != ""))
{
// Place the values on the form from the registry
textBoxMcAfeeVersion.Text = strMcAfeeVersion;
textBoxScanEngine.Text = strScanEngine;
textBoxDatVersion.Text = strDatFile;
buttonRun.Enabled = true;
}
else //null or empty
{
MessageBox.Show("This must be the first time that you have
used this program. Please set the values in the text boxes and click the
Save Button");
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if ((textBoxMcAfeeVersion.Text == null || textBoxScanEngine.Text
== null || textBoxDatVersion.Text == null) || (textBoxMcAfeeVersion.Text ==
"" || textBoxScanEngine.Text == "" || textBoxDatVersion.Text == ""))
{
MessageBox.Show("Please fill out all of the text boxes
before trying to Save");
}
else
{
myRegistry.Write("McAfeeVersion", textBoxMcAfeeVersion.Text);
myRegistry.Write("ScanEngine", textBoxScanEngine.Text);
myRegistry.Write("DatFile", textBoxDatVersion.Text);
}
}
private void buttonRun_Click(object sender, EventArgs e)
{
if ((textBoxMcAfeeVersion.Text == null ||
textBoxScanEngine.Text == null || textBoxDatVersion.Text == null) ||
(textBoxMcAfeeVersion.Text == "" || textBoxScanEngine.Text == "" ||
textBoxDatVersion.Text == ""))
{
MessageBox.Show("Please fill out all of the text boxes
before trying to Run");
}
else
{
DirectoryInfo Dir = new DirectoryInfo(@"c:\SVirus_ver");
FileInfo[] txtFiles = Dir.GetFiles();
// Print out the names of the files in the current directory.
foreach (FileInfo filesTemp in txtFiles)
//MessageBox.Show(filesTemp.Name);
{
try
{
// Create an instance of StreamReader to read from
a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new
StreamReader(Path.Combine(@"c:\SVirus_ver",filesTemp.Name)))
{
String line;
// Read and display lines from the file until
the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
// Remove spaces
string RemovedSpacesLine = line.Replace(",
",",");
// Split the line into an string array
string[] LineFields =
RemovedSpacesLine.Split(',');
// Dat File, some are 8, some are 4. Trim
the long ones.
if (LineFields[9].Length > 4)
LineFields[9] =
LineFields[9].Substring(4, (LineFields[9].Length - 4));
// Convert the Array Member to an int
int DatVersion =
Convert.ToInt32(LineFields[9]);
// Create the limit for the dat files.
int DatFileLowLimit =
(Convert.ToInt32(textBoxDatVersion.Text) - 15);
if (DatVersion < DatFileLowLimit)
{
// Computer Name
MessageBox.Show(LineFields[3]);
// Software Version
MessageBox.Show(LineFields[8]);
// Scan Engine
MessageBox.Show(LineFields[11]);
// Dat File
MessageBox.Show(LineFields[9]);
}
}
} // End of Using
} // End of Try
catch (Exception except)
{
// Let the user know what went wrong.
//MessageBox.Show("The file could not be read:");
//MessageBox.Show(except.Message);
}
} // End of ForEach
} // End of If Else
} // End of Button Click
} // End of Class
} // End NameSpace
I have a Windows Form that I have 3 textboxes and some buttons. Below is
the code that I have implemented it reads a pile of files from a folder and
then reads each of the files. If the McAfee Dat file version is old (older
than 15 revisions) then it will output the machine name, Scan Engine, and dat
file version (all of this is each of the text files that I load)
Is the way that I coded it poor C# OOP? I mean I have a registry class that
I have borrowed and implemented "using" so that I have access to the class.
Is it a bad practice to have so much code after a button like I do in my Run
button??
BTW I am using C# Express 2005 Beta 1....
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Utility.ModifyRegistry; // Class borrowed from the Internet
using System.IO;
#endregion
namespace McAfeeVersionCheck
{
partial class MainForm : Form
{
// Create an instance of the the registry class. Needs to be here
// because of the Save button (which saves the stuff to Registry)
(SCOPE)
ModifyRegistry myRegistry = new ModifyRegistry();
public MainForm()
{
InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit(); // Close Program
}
private void MainForm_Load(object sender, EventArgs e)
{
// Create an instance of the class.
//ModifyRegistry myRegistry = new ModifyRegistry();
// Creates strings and stores a value for each
// Registry reads from HKLM\SOFTWARE\<ProgramName>
string strMcAfeeVersion = myRegistry.Read("McAfeeVersion");
string strScanEngine = myRegistry.Read("ScanEngine");
string strDatFile = myRegistry.Read("DatFile");
if ((strMcAfeeVersion != null && strScanEngine != null &&
strDatFile != null) && (strMcAfeeVersion != "" || strScanEngine != "" ||
strDatFile != ""))
{
// Place the values on the form from the registry
textBoxMcAfeeVersion.Text = strMcAfeeVersion;
textBoxScanEngine.Text = strScanEngine;
textBoxDatVersion.Text = strDatFile;
buttonRun.Enabled = true;
}
else //null or empty
{
MessageBox.Show("This must be the first time that you have
used this program. Please set the values in the text boxes and click the
Save Button");
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if ((textBoxMcAfeeVersion.Text == null || textBoxScanEngine.Text
== null || textBoxDatVersion.Text == null) || (textBoxMcAfeeVersion.Text ==
"" || textBoxScanEngine.Text == "" || textBoxDatVersion.Text == ""))
{
MessageBox.Show("Please fill out all of the text boxes
before trying to Save");
}
else
{
myRegistry.Write("McAfeeVersion", textBoxMcAfeeVersion.Text);
myRegistry.Write("ScanEngine", textBoxScanEngine.Text);
myRegistry.Write("DatFile", textBoxDatVersion.Text);
}
}
private void buttonRun_Click(object sender, EventArgs e)
{
if ((textBoxMcAfeeVersion.Text == null ||
textBoxScanEngine.Text == null || textBoxDatVersion.Text == null) ||
(textBoxMcAfeeVersion.Text == "" || textBoxScanEngine.Text == "" ||
textBoxDatVersion.Text == ""))
{
MessageBox.Show("Please fill out all of the text boxes
before trying to Run");
}
else
{
DirectoryInfo Dir = new DirectoryInfo(@"c:\SVirus_ver");
FileInfo[] txtFiles = Dir.GetFiles();
// Print out the names of the files in the current directory.
foreach (FileInfo filesTemp in txtFiles)
//MessageBox.Show(filesTemp.Name);
{
try
{
// Create an instance of StreamReader to read from
a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new
StreamReader(Path.Combine(@"c:\SVirus_ver",filesTemp.Name)))
{
String line;
// Read and display lines from the file until
the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
// Remove spaces
string RemovedSpacesLine = line.Replace(",
",",");
// Split the line into an string array
string[] LineFields =
RemovedSpacesLine.Split(',');
// Dat File, some are 8, some are 4. Trim
the long ones.
if (LineFields[9].Length > 4)
LineFields[9] =
LineFields[9].Substring(4, (LineFields[9].Length - 4));
// Convert the Array Member to an int
int DatVersion =
Convert.ToInt32(LineFields[9]);
// Create the limit for the dat files.
int DatFileLowLimit =
(Convert.ToInt32(textBoxDatVersion.Text) - 15);
if (DatVersion < DatFileLowLimit)
{
// Computer Name
MessageBox.Show(LineFields[3]);
// Software Version
MessageBox.Show(LineFields[8]);
// Scan Engine
MessageBox.Show(LineFields[11]);
// Dat File
MessageBox.Show(LineFields[9]);
}
}
} // End of Using
} // End of Try
catch (Exception except)
{
// Let the user know what went wrong.
//MessageBox.Show("The file could not be read:");
//MessageBox.Show(except.Message);
}
} // End of ForEach
} // End of If Else
} // End of Button Click
} // End of Class
} // End NameSpace