标签:文件目录 删除 System private lst 账龄 filesize year using
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace JACKTools
{
public partial class FileLibAge : Form
{
private List<FileAge> lst = new List<FileAge>();
private const double KB = 1024;
private const double MB = 1024 * 1024;
private const double GB = MB * 1024;
private const double TB = GB * 1024;
public FileLibAge()
{
InitializeComponent();
}
private void btn_tj_Click(object sender, EventArgs e)
{
//目录路径
lst.Clear();
lsv_show.Items.Clear();
String path = txt_dir.Text.Trim();
countfileage(path);
lst.Sort(new FileAge());
for (int i = 0; i < lst.Count; i++)
{
String size = "";
if (lst[i].filesize > TB)
size = Math.Round(lst[i].filesize / TB ,2)+ "TB";
else if (lst[i].filesize > GB)
size = Math.Round(lst[i].filesize / GB ,2)+ "GB";
else if (lst[i].filesize > MB)
size = Math.Round(lst[i].filesize / MB ,2)+ "MB";
else if (lst[i].filesize > KB)
size = Math.Round(lst[i].filesize / KB ,2)+ "KB";
else size = lst[i].filesize + "B";
lsv_show.Items.Add(new ListViewItem(new String[] { lst[i].year + "", lst[i].filecnt + "", size }));
}
}
private void countfileage(string filedir)
{
DirectoryInfo root = new DirectoryInfo(filedir);
foreach (FileInfo f in root.GetFiles())
{
//
if (File.Exists(f.FullName))
{
int year = f.LastWriteTime.Year;
if(rdn_month.Checked)
year = f.LastWriteTime.Year * 100 + f.LastWriteTime.Month;
else
year = f.LastWriteTime.Year;
int iidex = getindexoflsv(year);
if (iidex >= 0)
{
lst[iidex].filecnt++;
lst[iidex].filesize += f.Length;
}
else
{
FileAge tmp = new FileAge();
tmp.year = year;
tmp.filecnt = 1;
tmp.filesize = f.Length;
lst.Add(tmp);
}
}
}
foreach (DirectoryInfo d in root.GetDirectories())
countfileage(d.FullName);
}
private int getindexoflsv(int year)
{
for (int i = 0; i < lst.Count; i++)
{
if (lst[i].year == year)
return i;
}
return -1;
}
private void btnclear_Click(object sender, EventArgs e)
{
if (DateTime.Now.Year - dtpend.Value.Year <= 1)
{
MessageBox.Show("1年内的数据不删除!");
return;
}
else
{
String path = txt_dir.Text.Trim();
cleardata(path);
MessageBox.Show("路径" + path + "下历史数据已经清理完毕!");
}
}
/// <summary>
/// 清理制定日期前的制定目录下的文件
/// </summary>
/// <param name="path"></param>
private void cleardata(String path)
{
DirectoryInfo root = new DirectoryInfo(path);
foreach (FileInfo f in root.GetFiles())
{
try
{
if (f.LastWriteTime < dtpend.Value)
f.Delete();
}
catch (Exception ee)
{
}
}
foreach (DirectoryInfo d in root.GetDirectories())
cleardata(d.FullName);
//路径下无文件 则直接删除目录
if (root.GetFiles().Length <= 0 && root.GetDirectories().Length <= 0)
{
root.Delete();
}
}
}
public class FileAge : IComparer<FileAge>
{
public int year
{
get;
set;
}
public int filecnt
{
get;
set;
}
public double filesize
{
get;
set;
}
//Compare函数
public int Compare(FileAge x, FileAge y)
{
try
{
if (x == null || y == null)
return 0;
return x.year.CompareTo(y.year);//升序
}
catch (Exception ee)
{ }
return 0;
}
}
}
标签:文件目录,删除,System,private,lst,账龄,filesize,year,using 来源: https://blog.csdn.net/xiaofei2008gxh/article/details/122716184
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。