公司里边有一条test case是检查提供给客户的文件里边没有空的文件夹, 我自己就做了个小工具来跑这条 case. 基础就是利用.NET 3.5中的SystemFileInfo类来调区disk上的信息:
界面如下:
代码如下:
代码
namespace CheckEmpty
{
public partial class CheckEmpty : Form
{
public CheckEmpty()
{
InitializeComponent();
}
private void Open_Click(object sender, EventArgs e)
{
oFDialog.ShowDialog();
if (DialogResult.Cancel != oFDialog.ShowDialog())
{
CheckPath.Text = oFDialog.FileName;
}
}
private void Save_Click(object sender, EventArgs e)
{
if (DialogResult.Cancel != sFDialog.ShowDialog())
{
outPutPath.Text = sFDialog.FileName;
}
}
private void Check_Click(object sender, EventArgs e)
{
if (CheckPath.Text.Trim() != "" && outPutPath.Text.Trim() != "")
{
try
{
Errlbl.Text = "";
toolStripProgressBar1.Value = 0;
string orfile = CheckPath.Text.Trim();
string reportPath = outPutPath.Text.Trim();
if (!File.Exists(reportPath))
{
// Errlbl.Text = "Output File path not exist";
FileStream fs = File.Create(reportPath);
fs.Close();
}
int MaxCount = 0;
int EmptyCount = 0;
//Count total folders
CountfiletoScan(orfile, ref MaxCount);
if (MaxCount != 0)
{
toolStripProgressBar1.Maximum = MaxCount;
}
StreamWriter sw = File.CreateText(reportPath);
sw.WriteLine(string.Format("Following folder are empty: (Total folders: {0})", MaxCount));
// Scan empty folders
RecursiveScan(orfile, sw, ref EmptyCount);
sw.Close();
sw.Dispose();
Errlbl.ForeColor = Color.Red;
if (MaxCount != 0)
{
Errlbl.Text = string.Format(@"Completed! emptys({0})", EmptyCount);
}
else
{
Errlbl.Text = "Completed! Err";
}
Clipboard.SetDataObject(reportPath); //Copy output report to clipboard
}
catch { }
}
else
{
Errlbl.Text = "Both Folder and Report can not be empty";
}
}
private void RecursiveScan(string dir, StreamWriter sw, ref int emptycount)
{
DirectoryInfo di = new DirectoryInfo(dir);
if (!di.Exists)
{
//Original directory not exist
sw.WriteLine(string.Format (@"dirctory '{0}' not exist",di.FullName));
sw.Flush();
toolStripProgressBar1.Value = 100;
}
else
{
try
{
FileSystemInfo[] fsi = di.GetFileSystemInfos();
toolStripProgressBar1.Value += di.GetDirectories().Length;
if (fsi.Length == 0)
{
//Output empty direcotry
sw.WriteLine(di.FullName);
sw.Flush();
emptycount++;
}
else
{
foreach (DirectoryInfo item in di.GetDirectories())
{
RecursiveScan(item.FullName, sw, ref emptycount);
}
}
}
catch (FieldAccessException fae)
{
//
}
catch (Exception ex)
{
//
}
}
}
private void CountfiletoScan(string dir, ref int count)
{
DirectoryInfo di = new DirectoryInfo(dir);
if (!di.Exists)
{
}
else
{
try
{
DirectoryInfo[] dis = di.GetDirectories();
count += dis.Length;
foreach (DirectoryInfo item in dis)
{
CountfiletoScan(item.FullName, ref count);
}
}
catch
{ }
}
}
}
}
{
public partial class CheckEmpty : Form
{
public CheckEmpty()
{
InitializeComponent();
}
private void Open_Click(object sender, EventArgs e)
{
oFDialog.ShowDialog();
if (DialogResult.Cancel != oFDialog.ShowDialog())
{
CheckPath.Text = oFDialog.FileName;
}
}
private void Save_Click(object sender, EventArgs e)
{
if (DialogResult.Cancel != sFDialog.ShowDialog())
{
outPutPath.Text = sFDialog.FileName;
}
}
private void Check_Click(object sender, EventArgs e)
{
if (CheckPath.Text.Trim() != "" && outPutPath.Text.Trim() != "")
{
try
{
Errlbl.Text = "";
toolStripProgressBar1.Value = 0;
string orfile = CheckPath.Text.Trim();
string reportPath = outPutPath.Text.Trim();
if (!File.Exists(reportPath))
{
// Errlbl.Text = "Output File path not exist";
FileStream fs = File.Create(reportPath);
fs.Close();
}
int MaxCount = 0;
int EmptyCount = 0;
//Count total folders
CountfiletoScan(orfile, ref MaxCount);
if (MaxCount != 0)
{
toolStripProgressBar1.Maximum = MaxCount;
}
StreamWriter sw = File.CreateText(reportPath);
sw.WriteLine(string.Format("Following folder are empty: (Total folders: {0})", MaxCount));
// Scan empty folders
RecursiveScan(orfile, sw, ref EmptyCount);
sw.Close();
sw.Dispose();
Errlbl.ForeColor = Color.Red;
if (MaxCount != 0)
{
Errlbl.Text = string.Format(@"Completed! emptys({0})", EmptyCount);
}
else
{
Errlbl.Text = "Completed! Err";
}
Clipboard.SetDataObject(reportPath); //Copy output report to clipboard
}
catch { }
}
else
{
Errlbl.Text = "Both Folder and Report can not be empty";
}
}
private void RecursiveScan(string dir, StreamWriter sw, ref int emptycount)
{
DirectoryInfo di = new DirectoryInfo(dir);
if (!di.Exists)
{
//Original directory not exist
sw.WriteLine(string.Format (@"dirctory '{0}' not exist",di.FullName));
sw.Flush();
toolStripProgressBar1.Value = 100;
}
else
{
try
{
FileSystemInfo[] fsi = di.GetFileSystemInfos();
toolStripProgressBar1.Value += di.GetDirectories().Length;
if (fsi.Length == 0)
{
//Output empty direcotry
sw.WriteLine(di.FullName);
sw.Flush();
emptycount++;
}
else
{
foreach (DirectoryInfo item in di.GetDirectories())
{
RecursiveScan(item.FullName, sw, ref emptycount);
}
}
}
catch (FieldAccessException fae)
{
//
}
catch (Exception ex)
{
//
}
}
}
private void CountfiletoScan(string dir, ref int count)
{
DirectoryInfo di = new DirectoryInfo(dir);
if (!di.Exists)
{
}
else
{
try
{
DirectoryInfo[] dis = di.GetDirectories();
count += dis.Length;
foreach (DirectoryInfo item in dis)
{
CountfiletoScan(item.FullName, ref count);
}
}
catch
{ }
}
}
}
}