using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Diagnostics;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Windows.Forms;
|
|
namespace CountFiles
|
{
|
public partial class FrmMain : Form
|
{
|
public FrmMain()
|
{
|
InitializeComponent();
|
}
|
|
private void btnCount_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
string path = this.txtPath.Text.Trim();
|
if (string.IsNullOrEmpty(path))
|
{
|
MessageBox.Show("请选择一个目录!");
|
this.txtPath.Focus();
|
}
|
if (!Directory.Exists(path))
|
{
|
MessageBox.Show("你选择的目录不存在!");
|
this.txtPath.Focus();
|
}
|
|
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
|
|
List<string> list = new List<string>();
|
foreach (string file in files)
|
{
|
//sizes += new FileInfo(file).Length;
|
FileInfo fi = new FileInfo(file);
|
list.Add(file + "," + FormatBytes(fi.Length));
|
}
|
|
string rs = Path.Combine(path, "统计结果.txt");
|
File.WriteAllLines(rs, list.ToArray());
|
|
MessageBox.Show("计算完成!");
|
OpenFolder(path);
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.Message, "出错");
|
}
|
}
|
|
public static string FormatBytes(long bytes)
|
{
|
string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" };
|
|
int i = 0;
|
double dblSByte = bytes;
|
if (bytes > 1024)
|
for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
|
dblSByte = bytes / 1024.0;
|
|
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
|
}
|
|
private static void OpenFolder(string folderPath)
|
{
|
if (string.IsNullOrEmpty(folderPath)) return;
|
|
Process process = new Process();
|
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
|
psi.Arguments = folderPath;
|
process.StartInfo = psi;
|
|
try
|
{
|
process.Start();
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
finally
|
{
|
process.Close();
|
}
|
}
|
}
|
}
|