//获得文件夹路径
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
this.FilePathTextBox.Text = fbd.SelectedPath;
GetFileInfo(this.FilePathTextBox.Text);
}
}
//取得所有子文件夹并循环对应的大小
private void GetFileInfo(string strPath)
{
try
{
DirectoryInfo theFolder = new DirectoryInfo(strPath);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in dirInfo)
{
string size = FormatBytes(this.GetFilesSize(Path.Combine(strPath, NextFolder.Name)));
this.listBox1.Items.Add(NextFolder.Name + "------" +size );
this.listView1.Items.Add(new ListViewItem ( new string[] {NextFolder.FullName, size}));
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
//取子文件夹下所有文件大小总和
public long GetFilesSize(String path)
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
long length = 0;
foreach (FileSystemInfo fsi in directoryInfo.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
length += ((FileInfo)fsi).Length;
}
else
{
length += GetFilesSize(fsi.FullName);
}
}
return length;
}
//大小转化成"GB", "MB", "KB", "Bytes"
public string FormatBytes(long bytes)
{
const int scale = 1024;
string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
long max = (long)Math.Pow(scale, orders.Length - 1);
foreach (string order in orders)
{
if (bytes > max)
{
return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
}
max /= scale;
}
return "0 Bytes";
}
-----------------------------------------
//获得保存文件路径
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = "fileName.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
}
-----------------------------------------
private void LogFilePathButton_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1 = new FolderBrowserDialog();
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.LogFilePathTextBox.Text = this.folderBrowserDialog1.SelectedPath;
DirectoryInfo mydir = new DirectoryInfo(this.LogFilePathTextBox.Text);
//フォルダ数
DirectoryInfo[] listdir = mydir.GetDirectories();
int count = listdir.Length;
//ファイル数
FileInfo[] listfile = mydir.GetFiles();
count = listfile.Length;
//ファイルの名前修正
foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
FileInfo fi = (FileInfo)fsi;
string LogFileName = System.IO.Path.GetDirectoryName(fi.FullName);
string s = System.IO.Path.GetExtension(fi.FullName);
string y = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
Console.WriteLine(y);
if (s == ".jpg")
{
//System.IO.File.Copy(fi.FullName, x + @"\oo" + fi.Name);
System.IO.File.Delete(fi.FullName);
}
}
}
}
else
{
return;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------