仿资源管理器功能实现
以前的一个项目myTree中仿资源管理器功能实现,,忘记的差不多了。可能是按照网络现成实例做的。
自定义功能函数:
myExtractIcon(...)和SetIcon(...)函数功能是获取文件夹和文件的图标。
GetDrive() 获取系统驱动器时调用。
AddDirectories(...) 获得当前目录下的所有目录。填充树壮控件
ListViewAB(...) 在大图标、小图标、详细列表切换时调用
InitList(...) 填充列表框
InitList2(...) 同InitList一样,只是参数不一样。在列表框中双击目录时调用。
主要事件
1:TreeView控件的BeforeExpand事件;该事件是TreeView控件的节点展开时发生。
2:TreeView控件的AfterSelect事件;该事件是TreeView控件的节点选中时发生。
3:ListView控件的ItemActivate事件。该事件是在ListView控件中双击选中的文件时发生。
在GetDrive()函数中将"G"光盘符,换成你电脑中的光盘符。
本程序主要应用了C#中的DirectoryInfo和FileInfo类。使用了2个获取文件图标的API函数。
代码如下:
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ListView listView1;
[DllImport("Shell32.dll")]
public static extern int ExtractIcon(IntPtr h,string strx,int ii);
[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public char szDisplayName;
public char szTypeName;
}
//在shell32.dll导入函数SHGetFileInfo
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes,
ref MyFileInfomation psfi, int cbFileInfo,int uFlags);
//定义SHFILEINFO结构(名字随便起,这里用MyFileInfomation)
[StructLayout(LayoutKind.Sequential)]
public struct MyFileInfomation
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}
//定义文件属性标识
public enum FileAttributeFlags : int
{
FILE_ATTRIBUTE_READONLY =0x00000001,
FILE_ATTRIBUTE_HIDDEN =0x00000002,
FILE_ATTRIBUTE_SYSTEM =0x00000004,
FILE_ATTRIBUTE_DIRECTORY =0x00000010,
FILE_ATTRIBUTE_ARCHIVE =0x00000020,
FILE_ATTRIBUTE_DEVICE =0x00000040,
FILE_ATTRIBUTE_NORMAL =0x00000080,
FILE_ATTRIBUTE_TEMPORARY =0x00000100,
FILE_ATTRIBUTE_SPARSE_FILE =0x00000200,
FILE_ATTRIBUTE_REPARSE_POINT =0x00000400,
FILE_ATTRIBUTE_COMPRESSED =0x00000800,
FILE_ATTRIBUTE_OFFLINE =0x00001000,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED =0x00002000,
FILE_ATTRIBUTE_ENCRYPTED =0x00004000
}
//定义获取资源标识
public enum GetFileInfoFlags : int
{
SHGFI_ICON =0x000000100, // get icon
SHGFI_DISPLAYNAME =0x000000200, // get display name
SHGFI_TYPENAME =0x000000400, // get type name
SHGFI_ATTRIBUTES =0x000000800, // get attributes
SHGFI_ICONLOCATION =0x000001000, // get icon location
SHGFI_EXETYPE =0x000002000, // return exe type
SHGFI_SYSICONINDEX =0x000004000, // get system icon index
SHGFI_LINKOVERLAY =0x000008000, // put a link overlay on icon
SHGFI_SELECTED =0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED =0x000020000, // get only specified attributes
SHGFI_LARGEICON =0x000000000, // get large icon
SHGFI_SMALLICON =0x000000001, // get small icon
SHGFI_OPENICON =0x000000002, // get open icon
SHGFI_SHELLICONSIZE =0x000000004, // get shell size icon
SHGFI_PIDL =0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES =0x000000010, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS =0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX =0x000000040 // Get the index of the overlay
}
//********************************************************************************
string strFilePath="";
//*************************************************************************************
protected virtual Icon myExtractIcon(string FileName,int iIndex)
{
try
{
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
if(! hIcon.Equals(null))
{
Icon icon=Icon.FromHandle(hIcon);
return icon;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
return null;
}
//*************************************************************************************
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
SHFILEINFO fi=new SHFILEINFO();
if(tf==true)
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
else
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
// return null;
}
//*************************************************************************************
构造函数中 :
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",15);
TreeImageList.Images.Add(ic0);
Icon ic1=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",5);
TreeImageList.Images.Add(ic1);
Icon ic2=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",7);
TreeImageList.Images.Add(ic2);
Icon ic3=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",11);
TreeImageList.Images.Add(ic3);
Icon ic4=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
TreeImageList.Images.Add(ic4);
Icon ic5=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",4);
TreeImageList.Images.Add(ic5);
Icon ic6=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",101);
TreeImageList.Images.Add(ic6);
this.directoryPath=@Application.StartupPath;
GetDrive();
//*************************************************************************************
public void GetDrive()
{
treeView1.ImageList=TreeImageList;
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(this.directoryPath);
if (info.Exists)
{
rootNode = new TreeNode(info.Name,4,5);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
this.treeView1.Nodes.Add(rootNode);
this.pre_Path=@RemoveInfoName();
}
}
//*************
private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name,4, 5);
aNode.Tag = subDir;
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
private string RemoveInfoName()
{
string disk=string.Empty;
DirectoryInfo info = new DirectoryInfo(this.directoryPath);
if (info.Exists)
{
int index1=this.directoryPath.IndexOf(info.Name);
disk=this.directoryPath.Substring(0,index1);
}
return disk;
}
void AddDirectories(TreeNode tn)
{
tn.Nodes.Clear();
string strPath=@RemoveInfoName();
strPath=strPath.Remove(0,5);
//获得当前目录
DirectoryInfo dirinfo = new DirectoryInfo(strPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{ return;}
int iImageIndex=4; int iSelectedIndex=5;
foreach (DirectoryInfo di in adirinfo)
{
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")
{iImageIndex=6; iSelectedIndex=6;}
else
{iImageIndex=4; iSelectedIndex=5;}
TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
tn.Nodes.Add(tnDir);
}
}
//*************************************************************************************
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Node.Text=="我的电脑")
{ return;}
InitList(e.Node);
}
//*************************************************************************************
protected virtual void InitList(TreeNode tn)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等..";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
listView1.Columns.Add("文件类型",160,HorizontalAlignment.Left);
string strPath=this.pre_Path+this.treeView1.SelectedNode.FullPath;
// strPath=strPath.Remove(0,5);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strPath) ;//);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[5];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
// string flex =System.IO.Path.GetExtension(strFileName);
arrSubItem[4]=GetTypeName(strFileName); //fileInfo.Attributes.ToString();
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;
}
strFilePath=strPath;
textBox1.Text=strPath;
this.statusBarPanel1.Text=strPath;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;
DirectoryInfo Dir=new DirectoryInfo(strPath);
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
protected virtual void InitList2(string strName)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等..";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
listView1.Columns.Add("文件类型",160,HorizontalAlignment.Left);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[5];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
arrSubItem[4]=GetTypeName(strFileName);
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;//必须加在listView1.Items.Insert(iCount,LiItem);
}
strFilePath=strName;//把路径赋值于全局变量strFilePath
textBox1.Text=strName;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
DirectoryInfo Dir=new DirectoryInfo(strName);//创建目录对象。
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
//*************************************************************************************
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);
try
{
if(listView1.FocusedItem.SubItems.Count>1)
{
System.Diagnostics.Process.Start(str);
}
else
{ InitList2(str); }
}
catch { return;}
}
//*************************************************************************************
private string GetTypeName(string fileName)
{
MyFileInfomation fileInfo = new MyFileInfomation();//初始化MyFileInfomation结构
//调用GetFileInfo函数,最后一个参数说明获取的是文件类型(SHGFI_TYPENAME)
int res = GetFileInfo(fileName, (int)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
ref fileInfo, Marshal.SizeOf(fileInfo), (int)GetFileInfoFlags.SHGFI_TYPENAME);
return fileInfo.szTypeName;
}
private System.Windows.Forms.ListView listView1;
[DllImport("Shell32.dll")]
public static extern int ExtractIcon(IntPtr h,string strx,int ii);
[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public char szDisplayName;
public char szTypeName;
}
//在shell32.dll导入函数SHGetFileInfo
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes,
ref MyFileInfomation psfi, int cbFileInfo,int uFlags);
//定义SHFILEINFO结构(名字随便起,这里用MyFileInfomation)
[StructLayout(LayoutKind.Sequential)]
public struct MyFileInfomation
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}
//定义文件属性标识
public enum FileAttributeFlags : int
{
FILE_ATTRIBUTE_READONLY =0x00000001,
FILE_ATTRIBUTE_HIDDEN =0x00000002,
FILE_ATTRIBUTE_SYSTEM =0x00000004,
FILE_ATTRIBUTE_DIRECTORY =0x00000010,
FILE_ATTRIBUTE_ARCHIVE =0x00000020,
FILE_ATTRIBUTE_DEVICE =0x00000040,
FILE_ATTRIBUTE_NORMAL =0x00000080,
FILE_ATTRIBUTE_TEMPORARY =0x00000100,
FILE_ATTRIBUTE_SPARSE_FILE =0x00000200,
FILE_ATTRIBUTE_REPARSE_POINT =0x00000400,
FILE_ATTRIBUTE_COMPRESSED =0x00000800,
FILE_ATTRIBUTE_OFFLINE =0x00001000,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED =0x00002000,
FILE_ATTRIBUTE_ENCRYPTED =0x00004000
}
//定义获取资源标识
public enum GetFileInfoFlags : int
{
SHGFI_ICON =0x000000100, // get icon
SHGFI_DISPLAYNAME =0x000000200, // get display name
SHGFI_TYPENAME =0x000000400, // get type name
SHGFI_ATTRIBUTES =0x000000800, // get attributes
SHGFI_ICONLOCATION =0x000001000, // get icon location
SHGFI_EXETYPE =0x000002000, // return exe type
SHGFI_SYSICONINDEX =0x000004000, // get system icon index
SHGFI_LINKOVERLAY =0x000008000, // put a link overlay on icon
SHGFI_SELECTED =0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED =0x000020000, // get only specified attributes
SHGFI_LARGEICON =0x000000000, // get large icon
SHGFI_SMALLICON =0x000000001, // get small icon
SHGFI_OPENICON =0x000000002, // get open icon
SHGFI_SHELLICONSIZE =0x000000004, // get shell size icon
SHGFI_PIDL =0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES =0x000000010, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS =0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX =0x000000040 // Get the index of the overlay
}
//********************************************************************************
string strFilePath="";
//*************************************************************************************
protected virtual Icon myExtractIcon(string FileName,int iIndex)
{
try
{
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
if(! hIcon.Equals(null))
{
Icon icon=Icon.FromHandle(hIcon);
return icon;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
return null;
}
//*************************************************************************************
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
{
SHFILEINFO fi=new SHFILEINFO();
if(tf==true)
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
else
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
// return null;
}
//*************************************************************************************
构造函数中 :
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",15);
TreeImageList.Images.Add(ic0);
Icon ic1=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",5);
TreeImageList.Images.Add(ic1);
Icon ic2=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",7);
TreeImageList.Images.Add(ic2);
Icon ic3=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",11);
TreeImageList.Images.Add(ic3);
Icon ic4=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
TreeImageList.Images.Add(ic4);
Icon ic5=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",4);
TreeImageList.Images.Add(ic5);
Icon ic6=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",101);
TreeImageList.Images.Add(ic6);
this.directoryPath=@Application.StartupPath;
GetDrive();
//*************************************************************************************
public void GetDrive()
{
treeView1.ImageList=TreeImageList;
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(this.directoryPath);
if (info.Exists)
{
rootNode = new TreeNode(info.Name,4,5);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
this.treeView1.Nodes.Add(rootNode);
this.pre_Path=@RemoveInfoName();
}
}
//*************
private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name,4, 5);
aNode.Tag = subDir;
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
private string RemoveInfoName()
{
string disk=string.Empty;
DirectoryInfo info = new DirectoryInfo(this.directoryPath);
if (info.Exists)
{
int index1=this.directoryPath.IndexOf(info.Name);
disk=this.directoryPath.Substring(0,index1);
}
return disk;
}
void AddDirectories(TreeNode tn)
{
tn.Nodes.Clear();
string strPath=@RemoveInfoName();
strPath=strPath.Remove(0,5);
//获得当前目录
DirectoryInfo dirinfo = new DirectoryInfo(strPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{ return;}
int iImageIndex=4; int iSelectedIndex=5;
foreach (DirectoryInfo di in adirinfo)
{
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")
{iImageIndex=6; iSelectedIndex=6;}
else
{iImageIndex=4; iSelectedIndex=5;}
TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
tn.Nodes.Add(tnDir);
}
}
//*************************************************************************************
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Node.Text=="我的电脑")
{ return;}
InitList(e.Node);
}
//*************************************************************************************
protected virtual void InitList(TreeNode tn)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等..";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
listView1.Columns.Add("文件类型",160,HorizontalAlignment.Left);
string strPath=this.pre_Path+this.treeView1.SelectedNode.FullPath;
// strPath=strPath.Remove(0,5);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strPath) ;//);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[5];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
// string flex =System.IO.Path.GetExtension(strFileName);
arrSubItem[4]=GetTypeName(strFileName); //fileInfo.Attributes.ToString();
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;
}
strFilePath=strPath;
textBox1.Text=strPath;
this.statusBarPanel1.Text=strPath;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;
DirectoryInfo Dir=new DirectoryInfo(strPath);
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
protected virtual void InitList2(string strName)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等..";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
listView1.Columns.Add("文件类型",160,HorizontalAlignment.Left);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[5];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
arrSubItem[4]=GetTypeName(strFileName);
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;//必须加在listView1.Items.Insert(iCount,LiItem);
}
strFilePath=strName;//把路径赋值于全局变量strFilePath
textBox1.Text=strName;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
DirectoryInfo Dir=new DirectoryInfo(strName);//创建目录对象。
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
//*************************************************************************************
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);
try
{
if(listView1.FocusedItem.SubItems.Count>1)
{
System.Diagnostics.Process.Start(str);
}
else
{ InitList2(str); }
}
catch { return;}
}
//*************************************************************************************
private string GetTypeName(string fileName)
{
MyFileInfomation fileInfo = new MyFileInfomation();//初始化MyFileInfomation结构
//调用GetFileInfo函数,最后一个参数说明获取的是文件类型(SHGFI_TYPENAME)
int res = GetFileInfo(fileName, (int)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
ref fileInfo, Marshal.SizeOf(fileInfo), (int)GetFileInfoFlags.SHGFI_TYPENAME);
return fileInfo.szTypeName;
}