记录八月份写的一个小工具,当需要处理的很多数据在文件名中存在时,
1,使用Directory类下的GetDirectories方法根据文件地址打开文件获取目录。
2,然后再使用DirectoryInfo类下的GetFiles获取具体文件。
3,根据具体文件获取文件名,大小等信息。
4,导出至EXCEL
命名空间注意:
File类需要System.IO
Excel需要Aspose.Cells
一、选择文件夹返回文件地址
private void btn_Select_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBDialog = new FolderBrowserDialog();
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
txt_File.Text = strPath;
}
}
二、遍历获取当前文件夹下的文件信息
winform窗体中可以有一个ListViewItem组件,便于返回文件信息。
private void button2_Click(object sender, EventArgs e)
{
try
{
DirectoryInfo dir2 = new DirectoryInfo(txt_File.Text);
foreach (FileInfo file in dir2.GetFiles("*.wav"))
{
ListViewItem item = new ListViewItem();
// strAA = strAA + file.Name +" "+file.Length/1024+"kb"+ "\r\n";
item.Text = file.Name.Substring(0, 8);
item.SubItems.Add(file.Name.Substring(9