using System.IO;
using System.Management;
public class BindFiles
{
private ListView listView1;
private static string fileType;
public BindFiles(ListView view)
{
listView1 = view;
}
/// <summary>
/// 绑定listView的内容
/// </summary>
/// <param name="dir">要绑定文件(夹)的所在路径</param>
public void BindListView( string dir)
{
this.listView1.Items.Clear();
string[] cats = new string[0];
string[] files = new string[0];
try { cats = Directory.GetDirectories(dir); }
catch { }
try { files = Directory.GetFiles(dir); }
catch { }
if(cats.Length>0) this.BindCatelog(cats);
if(files.Length>0) this.BindFile(cats.Length,files);
}
//绑定文件夹
private void BindCatelog( string[] cats)
{
int i = 0;
foreach( string cat in cats)
{
this.listView1.Items.Add(cat.Substring(cat.LastIndexOf("//")+1));
this.listView1.Items[i].Tag = cat+"//";
this.listView1.Items[i].ImageIndex = 1;
this.listView1.Items[i].SubItems.Add("");
this.listView1.Items[i].SubItems.Add("文件夹");
this.listView1.Items[i].SubItems.Add(Directory.GetLastAccessTime(cat).ToShortDateString());
i ++;
}
}
//绑定文件
private void BindFile( int index, string[] files)
{
int i = index;
foreach( string file in files)
{
this.listView1.Items.Add(file.Substring(file.LastIndexOf("//")+1));
this.listView1.Items[i].Tag = file;
string ex = file.Substring(file.LastIndexOf(".")+1);
this.listView1.Items[i].ImageIndex = this.GetPic(ex);
string size = "不可访问";
try
{
FileStream fs = new FileStream(file,FileMode.Open);
size = fs.Length.ToString();
fs.Close();
if(size.Length>6)
size = size.Substring(0,size.Length-6)+"."+size.Substring(0,size.Length-5).Substring(size.Substring(0,size.Length-5).Length-1)+"MB";
else
size = size.Substring(0,size.Length-3) +"KB";
}
catch { }
this.listView1.Items[i].SubItems.Add(size);
this.listView1.Items[i].SubItems.Add(fileType);
this.listView1.Items[i].SubItems.Add(File.GetLastAccessTime(file).ToShortDateString());
i ++;
}
}
/// <summary>
/// 获得对应文件的图标
/// </summary>
/// <param name="ex">文件扩展名</param>
/// <returns>图标索引</returns>
private int GetPic( string ex)
{
int index = 12;
switch(ex)
{
case "bmp":
index = 2;
fileType = "bmp 图象";
break;
case "jpg":
case "jpe":
case "jpeg":
index = 3;
fileType = "jpg 图象";
break;
case "gif":
case "png":
index = 3;
fileType = "gif 图象";
break;
case "mdb":
index = 4;
fileType = "Access 应用程序";
break;
case "rar":
case "zip":
index = 5;
fileType = "WinRAR 压缩文件";
break;
case "mp3":
case "wma":
case "wmv":
case "ra":
case "rm":
case "rmvb":
case "wav":
index = 6;
fileType = "波形文件";
break;
case "doc":
index =7;
fileType = "Word 文档";
break;
case "xls":
index = 8;
fileType = "Excel 工作表";
break;
case "ppt":
index = 9;
fileType = "Powerpoint 演示文稿";
break;
case "pub":
index = 10;
fileType = "Publisher 文档";
break;
case "txt":
index = 11;
fileType = "文本文档";
break;
case "swf":
index = 12;
fileType = "flash 文件";
break;
case "htm":
case "html":
index = 13;
fileType = "html 文件";
break;
case "chm":
index = 14;
fileType = "已编译的 HTML 帮助文档";
break;
case "dll":
index = 15;
fileType = "应用程序扩展";
break;
case "exe":
index = 16;
fileType = "应用程序";
break;
case "ini":
index = 17;
fileType = "配置设置";
break;
case "asp":
index = 18;
fileType = "Active Server Page";
break;
default:
index = 19;
fileType = "未知";
break;
}
return index;
}
using System.Management;
public class BindFiles
{
private ListView listView1;
private static string fileType;
public BindFiles(ListView view)
{
listView1 = view;
}
/// <summary>
/// 绑定listView的内容
/// </summary>
/// <param name="dir">要绑定文件(夹)的所在路径</param>
public void BindListView( string dir)
{
this.listView1.Items.Clear();
string[] cats = new string[0];
string[] files = new string[0];
try { cats = Directory.GetDirectories(dir); }
catch { }
try { files = Directory.GetFiles(dir); }
catch { }
if(cats.Length>0) this.BindCatelog(cats);
if(files.Length>0) this.BindFile(cats.Length,files);
}
//绑定文件夹
private void BindCatelog( string[] cats)
{
int i = 0;
foreach( string cat in cats)
{
this.listView1.Items.Add(cat.Substring(cat.LastIndexOf("//")+1));
this.listView1.Items[i].Tag = cat+"//";
this.listView1.Items[i].ImageIndex = 1;
this.listView1.Items[i].SubItems.Add("");
this.listView1.Items[i].SubItems.Add("文件夹");
this.listView1.Items[i].SubItems.Add(Directory.GetLastAccessTime(cat).ToShortDateString());
i ++;
}
}
//绑定文件
private void BindFile( int index, string[] files)
{
int i = index;
foreach( string file in files)
{
this.listView1.Items.Add(file.Substring(file.LastIndexOf("//")+1));
this.listView1.Items[i].Tag = file;
string ex = file.Substring(file.LastIndexOf(".")+1);
this.listView1.Items[i].ImageIndex = this.GetPic(ex);
string size = "不可访问";
try
{
FileStream fs = new FileStream(file,FileMode.Open);
size = fs.Length.ToString();
fs.Close();
if(size.Length>6)
size = size.Substring(0,size.Length-6)+"."+size.Substring(0,size.Length-5).Substring(size.Substring(0,size.Length-5).Length-1)+"MB";
else
size = size.Substring(0,size.Length-3) +"KB";
}
catch { }
this.listView1.Items[i].SubItems.Add(size);
this.listView1.Items[i].SubItems.Add(fileType);
this.listView1.Items[i].SubItems.Add(File.GetLastAccessTime(file).ToShortDateString());
i ++;
}
}
/// <summary>
/// 获得对应文件的图标
/// </summary>
/// <param name="ex">文件扩展名</param>
/// <returns>图标索引</returns>
private int GetPic( string ex)
{
int index = 12;
switch(ex)
{
case "bmp":
index = 2;
fileType = "bmp 图象";
break;
case "jpg":
case "jpe":
case "jpeg":
index = 3;
fileType = "jpg 图象";
break;
case "gif":
case "png":
index = 3;
fileType = "gif 图象";
break;
case "mdb":
index = 4;
fileType = "Access 应用程序";
break;
case "rar":
case "zip":
index = 5;
fileType = "WinRAR 压缩文件";
break;
case "mp3":
case "wma":
case "wmv":
case "ra":
case "rm":
case "rmvb":
case "wav":
index = 6;
fileType = "波形文件";
break;
case "doc":
index =7;
fileType = "Word 文档";
break;
case "xls":
index = 8;
fileType = "Excel 工作表";
break;
case "ppt":
index = 9;
fileType = "Powerpoint 演示文稿";
break;
case "pub":
index = 10;
fileType = "Publisher 文档";
break;
case "txt":
index = 11;
fileType = "文本文档";
break;
case "swf":
index = 12;
fileType = "flash 文件";
break;
case "htm":
case "html":
index = 13;
fileType = "html 文件";
break;
case "chm":
index = 14;
fileType = "已编译的 HTML 帮助文档";
break;
case "dll":
index = 15;
fileType = "应用程序扩展";
break;
case "exe":
index = 16;
fileType = "应用程序";
break;
case "ini":
index = 17;
fileType = "配置设置";
break;
case "asp":
index = 18;
fileType = "Active Server Page";
break;
default:
index = 19;
fileType = "未知";
break;
}
return index;
}
}
作者:朱会震