Brief Information about the November 2014 CSIG Meeting
WFsearch.cs - [2.30] Windows App for File Searching Whole Hard Drive by B. Arnold
LATE BREAKING NEWS: http://www.techsupportalert.com/ content/ microsoft-visual-studio- now-free-non-commercial-use.htm
(General Csig Info) For this
latest iteration of the search program we will briefly discuss some of the
anomalies that plagued the earlier versions. "It a feature, not a bug". We
will then move on to handling some basic operations of Windows text boxes. As
was noted last month, the program has been re-coded for
C-sharp Windows Forms. Names and functions have been copied from original source
program. No algorithmic changes were required. The original console program was a perfect model
for the Windows GUI version. The original console out functions now direct
data to a window. It's interesting to note that the code seems smaller. See
earlier months for descriptions of the development progress.
Additional
technical features: A Multi-Thread BackgroundWorker has been added. The
BackgroundWorker class allows running an operation on a separate, dedicated
thread. Here, it's the actual hard drive search. This enables the GUI to
continue responding to the user while simultaneously the disk is being scanned.
Sample Showing Double-Click Action
Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs. (Some are FREE!)
This month's program includes the following.
This is a CLI DOT NET program is coded using C-sharp (C#) and is very similar to C++. It also still uses the same DOT NET libraries.
Error Handling | Windows Forms | Try and Catch | TextBox I/O |
String Format | DirectoryInfo | Exceptions | Application |
DoEvents | EnumerateFiles | Recursive | ToString |
DoWork | ProgressChanged | RunWorkerCompleted | RunWorkerAsync |
There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.
Microsoft .Net Framework 4.5 |
C++ 9.0, etc. |
.Net 4.5, etc. |
CLI |
Common Language Infrastructure |
Sample Code
namespace WFsearch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeConsole();
Path.Text = "%HOMEPATH%";
}
// ------- Common variables (globals) ---------------------
int ncount, ndircount, ncatcherror;
long nsumsizes;
bool exitHit;
public void InitializeConsole()
{
this.mConsole.Clear();
this.mConsole.AppendText(
" WFsearch - Version 2.00 Oct. 2014 by B.Arnold\r\n" +
" Scans all or part of a drive for files matching a given criteria. \r\n\r\n" +
" 1. Some simple examples: (Copy/Paste works.)\r\n" +
" Ie. Start Here: C:\\ Search for: *.doc \r\n" +
" Ie. Start Here: C:\\ Search for: *2014*.log \r\n" +
" Ie. Start Here: . Search for: *.jpg \r\n" +
" 2. Enviornment variables may be used: \r\n" +
" Ie. Start Here: %HOMEPATH% Search for: *.jpg\r\n" +
" Others include %TEMP%, %APPDATA%, ETC. (Note ENVR button.)\r\n" +
" 3. Wildcard search specifiers \r\n" +
" * --- zero or more characters in that position. \r\n" +
" ? --- zero or one character in that position. \r\n" +
" anything else is considered a literal character. \r\n" +
"---------------------------\r\n\r\n");
InitGlobals();
this.mQuery.Focus();
}
//
// SEARCH and DISPLAY RESULTS
//
private void SearchAndResults()
{
this.mConsole.AppendText("\r\nStarting Search ...\r\n\r\n");
if (Directory.Exists(this.Path.Text)) ++ndircount;
DateTime begin = DateTime.UtcNow;
Process(Path.Text); // Launch recursive (RECURSIVE) algorithm
DateTime end = DateTime.UtcNow;
if (exitHit)
{ this.mConsole.AppendText("\r\nS E A R C H A B O R T E D\r\n\r\n"); }
else
{ this.mConsole.AppendText("\r\nD O N E\r\n\r\n"); }
// Display a complete summary.
//
this.dircount.Text = ndircount.ToString();
this.catcherror.Text = this.ncatcherror.ToString();
this.Timer.Text = String.Format("{0:n}", (end - begin).TotalSeconds);
this.count.Text = this.ncount.ToString();
this.Bytes.Text = String.Format("{0:n0} {1}", nsumsizes, addComment(nsumsizes));
}
//
// addComment ----- Emphasize the count size and units.
//
private string addComment(long arg)
{
if ((arg >> 50) > 0) return " ( " + (arg >> 50).ToString() + " PETABYTES )";
else if ((arg >> 40) > 0) return " ( " + (arg >> 40).ToString() + " TERABYTES )";
else if ((arg >> 30) > 0) return " ( " + (arg >> 30).ToString() + " GIGABYTES )";
else if ((arg >> 20) > 0) return " ( " + (arg >> 20).ToString() + " MEGABYTES )";
else if ((arg >> 10) > 0) return " ( " + (arg >> 10).ToString() + " KILOBYTES )";
else return "";
}
/// /////////////////////////// ////////////////////////////////// ////////////////////////// /////////////////////////
/// /////////////////////////// R E C U R S I V E M E T H O D ///////////////////////// ///////////////////////
/// /////////////////////////// ////////////////////////////////// ////////////////////////// /////////////////////////
private void Process(string root) // modified from support.microsoft.com/kb/303974
{
showActivity();
try
{
if (exitHit == true)
{
return;
}
try
{
string[] tree = Directory.GetFiles(root, mQuery.Text);
FileInfo info;
foreach (string f in tree)
{
info = new FileInfo(f);
this.mConsole.AppendText(f);
this.mConsole.AppendText("\r\n");
++ncount;
Application.DoEvents();
try
{
nsumsizes += info.Length;
}
catch
{
; // Ignore any file length errors.
}
}
}
catch (System.UnauthorizedAccessException uae)
{
//errorWriter.WriteLine(" [GetFiles()]: " + uae.Message);
++ncatcherror;
return;
}
catch (System.IO.DirectoryNotFoundException dnf)
{
++ncatcherror;
this.mConsole.AppendText(" [Not Found]: ");
this.mConsole.AppendText(dnf.Message);
this.mConsole.AppendText("\r\n");
return;
}
string[] dirtree = Directory.GetDirectories(root);
foreach (string d in dirtree)
{
++ndircount;
Process(d); // RECURSIVE ! RECURSIVE ! RECURSIVE ! RECURSIVE ! RECURSIVE !
}
}
catch (System.Exception excpt)
{
this.mConsole.AppendText(" [General Exception]: ");
this.mConsole.AppendText(excpt.Message);
this.mConsole.AppendText("\r\n");
}
}
SOURCE CODE
For help, email me at b a r n o l d @ i e e e . o r gBack to C++ Main Page