Using system. IO;
Using system. Management;
Public class bindfiles
{
Private listview listview1;
Private Static string filetype;
Public bindfiles (listview view)
{
Listview1 = view;
}
/// <Summary>
/// Bind the content of the listview
/// </Summary>
/// <Param name = "dir"> path of the file to be bound (folder) </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 );
}
// Bind a folder
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 ("folder ");
This. listview1.items [I]. subitems. Add (directory. getlastaccesstime (CAT). tow.datestring ());
I ++;
}
}
// Bind a file
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 = "inaccessible ";
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). tow.datestring ());
I ++;
}
}
/// <Summary>
/// Get the icon of the corresponding file
/// </Summary>
/// <Param name = "ex"> file extension </param>
/// <Returns> icon index </returns>
Private int getpic (string ex)
{
Int Index = 12;
Switch (Ex)
{
Case "BMP ":
Index = 2;
Filetype = "BMP image ";
Break;
Case "jpg ":
Case "jpe ":
Case "Jpeg ":
Index = 3;
Filetype = "jpg image ";
Break;
Case "GIF ":
Case "PNG ":
Index = 3;
Filetype = "GIF image ";
Break;
Case "mdb ":
Index = 4;
Filetype = "Access Application ";
Break;
Case "RAR ":
Case "Zip ":
Index = 5;
Filetype = "WinRAR compressed file ";
Break;
Case "MP3 ":
Case "WMA ":
Case "WMV ":
Case "ra ":
Case "RM ":
Case "rmvb ":
Case "WAV ":
Index = 6;
Filetype = "waveform file ";
Break;
Case "Doc ":
Index = 7;
Filetype = "Word Document ";
Break;
Case "xls ":
Index = 8;
Filetype = "Excel worksheet ";
Break;
Case "ppt ":
Index = 9;
Filetype = "PowerPoint presentation ";
Break;
Case "pub ":
Index = 10;
Filetype = "publisher document ";
Break;
Case "TXT ":
Index = 11;
Filetype = "Text Document ";
Break;
Case "SWF ":
Index = 12;
Filetype = "Flash file ";
Break;
Case "htm ":
Case "html ":
Index = 13;
Filetype = "HTML file ";
Break;
Case "CHM ":
Index = 14;
Filetype = "Compiled HTML Help document ";
Break;
Case "DLL ":
Index = 15;
Filetype = "application extension ";
Break;
Case "EXE ":
Index = 16;
Filetype = "application ";
Break;
Case "ini ":
Index = 17;
Filetype = "configuration settings ";
Break;
Case "asp ":
Index = 18;
Filetype = "Active Server Page ";
Break;
Default:
Index = 19;
Filetype = "unknown ";
Break;
}
Return Index;
}
}