仿资源管理器功能实现

以前的一个项目myTree中仿资源管理器功能实现,emsad.gif,忘记的差不多了。可能是按照网络现成实例做的。

自定义功能函数:
     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函数。
代码如下:

None.gif private  System.Windows.Forms.TreeView treeView1;
None.gif
private  System.Windows.Forms.ListView listView1;
None.gif
None.gif [DllImport(
" Shell32.dll " )] 
None.gif  
public   static   extern   int  ExtractIcon(IntPtr h, string  strx, int  ii);
None.gif
None.gif  [DllImport(
" Shell32.dll " )] 
None.gif  
public   static   extern   int  SHGetFileInfo( string  pszPath, uint  dwFileAttributes, ref  SHFILEINFO psfi, uint  cbFileInfo,  uint  uFlags);
None.gif
None.gif  
public   struct  SHFILEINFO
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif
InBlock.gif   
public IntPtr hIcon;  
InBlock.gif   
public int   iIcon;  
InBlock.gif   
public uint dwAttributes;
InBlock.gif   
public char szDisplayName; 
InBlock.gif   
public char szTypeName; 
ExpandedBlockEnd.gif  }

None.gif 
None.gif  
// 在shell32.dll导入函数SHGetFileInfo
None.gif
  [DllImport( " shell32.dll " , EntryPoint = " SHGetFileInfo " )]
None.gif  
public   static   extern   int  GetFileInfo( string  pszPath,  int  dwFileAttributes,
None.gif  
ref  MyFileInfomation psfi,  int  cbFileInfo, int  uFlags);
None.gif
None.gif  
// 定义SHFILEINFO结构(名字随便起,这里用MyFileInfomation)
None.gif
  [StructLayout(LayoutKind.Sequential)]
None.gif   
public   struct  MyFileInfomation
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif  
public IntPtr hIcon;
InBlock.gif  
public int iIcon;
InBlock.gif  
public int dwAttributes;
InBlock.gif
InBlock.gif  [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=260)]
InBlock.gif  
public string szDisplayName;
InBlock.gif
InBlock.gif  [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=80)]
InBlock.gif  
public string szTypeName;
ExpandedBlockEnd.gif  }

None.gif
None.gif  
// 定义文件属性标识
None.gif
   public   enum  FileAttributeFlags :  int
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif  FILE_ATTRIBUTE_READONLY      
=0x00000001,
InBlock.gif  FILE_ATTRIBUTE_HIDDEN              
=0x00000002,
InBlock.gif  FILE_ATTRIBUTE_SYSTEM              
=0x00000004,
InBlock.gif  FILE_ATTRIBUTE_DIRECTORY      
=0x00000010,
InBlock.gif  FILE_ATTRIBUTE_ARCHIVE       
=0x00000020,
InBlock.gif  FILE_ATTRIBUTE_DEVICE              
=0x00000040,
InBlock.gif  FILE_ATTRIBUTE_NORMAL              
=0x00000080,
InBlock.gif  FILE_ATTRIBUTE_TEMPORARY      
=0x00000100,
InBlock.gif  FILE_ATTRIBUTE_SPARSE_FILE     
=0x00000200,
InBlock.gif  FILE_ATTRIBUTE_REPARSE_POINT    
=0x00000400,
InBlock.gif  FILE_ATTRIBUTE_COMPRESSED          
=0x00000800,
InBlock.gif  FILE_ATTRIBUTE_OFFLINE       
=0x00001000,
InBlock.gif  FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 
=0x00002000,
InBlock.gif  FILE_ATTRIBUTE_ENCRYPTED      
=0x00004000
ExpandedBlockEnd.gif }

None.gif
None.gif 
// 定义获取资源标识
None.gif
  public   enum  GetFileInfoFlags :  int
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif  SHGFI_ICON              
=0x000000100,   // get icon
InBlock.gif
  SHGFI_DISPLAYNAME       =0x000000200,   // get display name
InBlock.gif
  SHGFI_TYPENAME          =0x000000400,   // get type name
InBlock.gif
  SHGFI_ATTRIBUTES        =0x000000800,   // get attributes
InBlock.gif
  SHGFI_ICONLOCATION      =0x000001000,   // get icon location
InBlock.gif
  SHGFI_EXETYPE           =0x000002000,   // return exe type
InBlock.gif
  SHGFI_SYSICONINDEX      =0x000004000,   // get system icon index
InBlock.gif
  SHGFI_LINKOVERLAY       =0x000008000,   // put a link overlay on icon
InBlock.gif
  SHGFI_SELECTED          =0x000010000,   // show icon in selected state
InBlock.gif
  SHGFI_ATTR_SPECIFIED    =0x000020000,   // get only specified attributes
InBlock.gif
  SHGFI_LARGEICON         =0x000000000,   // get large icon
InBlock.gif
  SHGFI_SMALLICON         =0x000000001,   // get small icon
InBlock.gif
  SHGFI_OPENICON          =0x000000002,   // get open icon
InBlock.gif
  SHGFI_SHELLICONSIZE     =0x000000004,   // get shell size icon
InBlock.gif
  SHGFI_PIDL              =0x000000008,   // pszPath is a pidl
InBlock.gif
  SHGFI_USEFILEATTRIBUTES =0x000000010,   // use passed dwFileAttribute
InBlock.gif
  SHGFI_ADDOVERLAYS       =0x000000020,   // apply the appropriate overlays
InBlock.gif
  SHGFI_OVERLAYINDEX      =0x000000040      // Get the index of the overlay
ExpandedBlockEnd.gif
}

None.gif
None.gif
// ********************************************************************************
None.gif

None.gif  
string  strFilePath = "" ;
None.gif  
// ************************************************************************************* 
None.gif
 
None.gif  
protected   virtual  Icon myExtractIcon( string  FileName, int  iIndex)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    IntPtr hIcon
=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
InBlock.gif    
if(! hIcon.Equals(null))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     Icon icon
=Icon.FromHandle(hIcon);
InBlock.gif     
return icon;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 
InBlock.gif   
return null;
ExpandedBlockEnd.gif  }

None.gif  
// *************************************************************************************
None.gif

None.gif  
protected   virtual   void  SetIcon(ImageList imageList, string  FileName, bool  tf)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   SHFILEINFO fi
=new SHFILEINFO();
InBlock.gif   
if(tf==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100,  16640);//SHGFI_ICON|SHGFI_SMALLICON
InBlock.gif
    try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(iTotal >0)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      Icon ic
=Icon.FromHandle(fi.hIcon);
InBlock.gif      imageList.Images.Add(ic);
InBlock.gif      
//return ic;
ExpandedSubBlockEnd.gif
     }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100,  257);
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(iTotal >0)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      Icon ic
=Icon.FromHandle(fi.hIcon);
InBlock.gif      imageList.Images.Add(ic);
InBlock.gif      
//return ic;
ExpandedSubBlockEnd.gif
     }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 
ExpandedSubBlockEnd.gif   }

InBlock.gif   
// return null;
ExpandedBlockEnd.gif
  }

None.gif  
// *************************************************************************************
None.gif

None.gif构造函数中 :
None.gif Icon ic0
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 15 );
None.gif   TreeImageList.Images.Add(ic0);
None.gif   Icon ic1
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 5 );
None.gif   TreeImageList.Images.Add(ic1);
None.gif   Icon ic2
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 7 );
None.gif   TreeImageList.Images.Add(ic2);
None.gif   Icon ic3
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 11 );
None.gif   TreeImageList.Images.Add(ic3);
None.gif
None.gif   Icon ic4
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 3 );
None.gif   TreeImageList.Images.Add(ic4);
None.gif   Icon ic5
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 4 );
None.gif   TreeImageList.Images.Add(ic5);
None.gif   Icon ic6
= myExtractIcon( " %SystemRoot%\\system32\\shell32.dll " , 101 );
None.gif   TreeImageList.Images.Add(ic6);
None.gif
None.gif
None.gif   
this .directoryPath = @Application.StartupPath;
None.gif   GetDrive();
None.gif
// *************************************************************************************
None.gif

None.gif  
public   void  GetDrive()
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   treeView1.ImageList
=TreeImageList;
InBlock.gif  
InBlock.gif   TreeNode rootNode;
InBlock.gif   
InBlock.gif   DirectoryInfo info 
= new DirectoryInfo(this.directoryPath); 
InBlock.gif   
if (info.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    rootNode 
= new TreeNode(info.Name,4,5);
InBlock.gif    rootNode.Tag 
= info;
InBlock.gif    GetDirectories(info.GetDirectories(), rootNode);
InBlock.gif    
this.treeView1.Nodes.Add(rootNode);
InBlock.gif    
this.pre_Path=@RemoveInfoName();
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif  
// *************
None.gif
private   void  GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   TreeNode aNode;
InBlock.gif   DirectoryInfo[] subSubDirs;
InBlock.gif   
foreach (DirectoryInfo subDir in subDirs)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    aNode 
= new TreeNode(subDir.Name,45);
InBlock.gif    aNode.Tag 
= subDir;
InBlock.gif    subSubDirs 
= subDir.GetDirectories();
InBlock.gif    
if (subSubDirs.Length != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     GetDirectories(subSubDirs, aNode);
ExpandedSubBlockEnd.gif    }

InBlock.gif    nodeToAddTo.Nodes.Add(aNode);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif
private   string  RemoveInfoName()
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
string disk=string.Empty;
InBlock.gif   DirectoryInfo info 
= new DirectoryInfo(this.directoryPath); 
InBlock.gif   
if (info.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
int index1=this.directoryPath.IndexOf(info.Name);
InBlock.gif    disk
=this.directoryPath.Substring(0,index1);
InBlock.gif    
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return disk;
ExpandedBlockEnd.gif  }

None.gif
None.gif
None.gif  
void  AddDirectories(TreeNode tn)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   tn.Nodes.Clear();
InBlock.gif   
string strPath=@RemoveInfoName();
InBlock.gif   strPath
=strPath.Remove(0,5);
InBlock.gif
InBlock.gif   
//获得当前目录
InBlock.gif
   DirectoryInfo   dirinfo = new DirectoryInfo(strPath);
InBlock.gif   DirectoryInfo[] adirinfo;
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    adirinfo 
= dirinfo.GetDirectories();
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gifreturn;}
InBlock.gif
InBlock.gif   
int iImageIndex=4;  int iSelectedIndex=5;
InBlock.gif   
foreach (DirectoryInfo di in adirinfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{iImageIndex=6;  iSelectedIndex=6;}
InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{iImageIndex=4;  iSelectedIndex=5;}
InBlock.gif
InBlock.gif    TreeNode tnDir 
= new TreeNode(di.Name, iImageIndex, iSelectedIndex);
InBlock.gif    tn.Nodes.Add(tnDir);
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedBlockEnd.gif  }

None.gif  
// *************************************************************************************
None.gif
  private   void  treeView1_AfterSelect( object  sender, System.Windows.Forms.TreeViewEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if(e.Node.Text=="我的电脑")    
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gifreturn;}
InBlock.gif
InBlock.gif   InitList(e.Node);
ExpandedBlockEnd.gif  }

None.gif
None.gif
// *************************************************************************************
None.gif

None.gif
protected   virtual   void  InitList(TreeNode tn)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
this.statusBarPanel1.Text="正在刷新文件夹,请稍等dot.gif..";
InBlock.gif   
this.Cursor=Cursors.WaitCursor;
InBlock.gif
InBlock.gif   
this.LisrimageList2.Images.Clear();
InBlock.gif   
this.LisrimageList.Images.Clear();
InBlock.gif   listView1.SmallImageList
=LisrimageList;
InBlock.gif   Icon ic0
=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
InBlock.gif   LisrimageList.Images.Add(ic0);
InBlock.gif   LisrimageList2.Images.Add(ic0);
InBlock.gif
InBlock.gif   listView1.Clear();
InBlock.gif   
//设置列表框的表头
InBlock.gif
   listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
InBlock.gif   listView1.Columns.Add(
"文件大小",120,HorizontalAlignment.Left);
InBlock.gif   listView1.Columns.Add(
"创建时间",120,HorizontalAlignment.Left);
InBlock.gif   listView1.Columns.Add(
"访问时间",200,HorizontalAlignment.Left);
InBlock.gif   listView1.Columns.Add(
"文件类型",160,HorizontalAlignment.Left);
InBlock.gif
InBlock.gif  
InBlock.gif   
string strPath=this.pre_Path+this.treeView1.SelectedNode.FullPath;
InBlock.gif
InBlock.gif
//   strPath=strPath.Remove(0,5);
InBlock.gif   
//获得当前目录下的所有文件 
InBlock.gif
   DirectoryInfo curDir=new DirectoryInfo(strPath) ;//);//创建目录对象。
InBlock.gif
   FileInfo[] dirFiles;
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dirFiles
=curDir.GetFiles();
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
catch  dot.gifreturn;}
InBlock.gif
InBlock.gif   
string []arrSubItem=new string[5];
InBlock.gif   
//文件的创建时间和访问时间。
InBlock.gif
   int iCount=0;    int iconIndex=1;//用1,而不用0是要让过0号图标。
InBlock.gif
   foreach(FileInfo fileInfo in dirFiles)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string strFileName=fileInfo.Name;           
InBlock.gif            
InBlock.gif    
//如果不是文件pagefile.sys
InBlock.gif
    if(! strFileName.Equals("pagefile.sys"))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     arrSubItem[
0]=strFileName;
InBlock.gif     arrSubItem[
1]=fileInfo.Length+" 字节";
InBlock.gif     arrSubItem[
2]=fileInfo.CreationTime.ToString();
InBlock.gif     arrSubItem[
3]=fileInfo.LastAccessTime.ToString();
InBlock.gif
//     string flex =System.IO.Path.GetExtension(strFileName);
InBlock.gif
     arrSubItem[4]=GetTypeName(strFileName);  //fileInfo.Attributes.ToString();
InBlock.gif

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
InBlock.gif   
InBlock.gif
InBlock.gif    
//得到每个文件的图标
InBlock.gif
    string str=fileInfo.FullName;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     SetIcon(LisrimageList,str,
false);
InBlock.gif     SetIcon(LisrimageList2,str,
true);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
InBlock.gif        
InBlock.gif
InBlock.gif    
//插入列表项    
InBlock.gif
    ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
InBlock.gif    listView1.Items.Insert(iCount,LiItem);
InBlock.gif
InBlock.gif    iCount
++
InBlock.gif    iconIndex
++;
ExpandedSubBlockEnd.gif   }

InBlock.gif   strFilePath
=strPath;
InBlock.gif   textBox1.Text
=strPath;
InBlock.gif   
this.statusBarPanel1.Text=strPath;
InBlock.gif   
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"";
InBlock.gif   
this.Cursor=Cursors.Arrow;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif   
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
InBlock.gif
   int iItem=0;
InBlock.gif
InBlock.gif   DirectoryInfo Dir
=new DirectoryInfo(strPath);
InBlock.gif   
foreach(DirectoryInfo di in Dir.GetDirectories())
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{   
InBlock.gif    ListViewItem LiItem
=new ListViewItem(di.Name,0);
InBlock.gif    listView1.Items.Insert(iItem,LiItem);
InBlock.gif    iItem
++;
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedBlockEnd.gif  }
 
None.gif
None.gif
None.gif    
protected   virtual   void  InitList2( string  strName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
this.statusBarPanel1.Text="正在刷新文件夹,请稍等..";
InBlock.gif            
this.Cursor=Cursors.WaitCursor;
InBlock.gif
InBlock.gif            
this.LisrimageList2.Images.Clear();
InBlock.gif            
this.LisrimageList.Images.Clear();
InBlock.gif            listView1.SmallImageList
=LisrimageList;
InBlock.gif            Icon ic0
=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
InBlock.gif            LisrimageList.Images.Add(ic0);
InBlock.gif            LisrimageList2.Images.Add(ic0);
InBlock.gif
InBlock.gif            listView1.Clear();
InBlock.gif            
//设置列表框的表头
InBlock.gif
            listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
InBlock.gif            listView1.Columns.Add(
"文件大小",120,HorizontalAlignment.Left);
InBlock.gif            listView1.Columns.Add(
"创建时间",120,HorizontalAlignment.Left);
InBlock.gif            listView1.Columns.Add(
"访问时间",200,HorizontalAlignment.Left);
InBlock.gif        listView1.Columns.Add(
"文件类型",160,HorizontalAlignment.Left);
InBlock.gif  
InBlock.gif            
//获得当前目录下的所有文件 
InBlock.gif
            DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。
InBlock.gif
            FileInfo[] dirFiles;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dirFiles
=curDir.GetFiles();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch  dot.gifreturn;}
InBlock.gif
InBlock.gif            
string []arrSubItem=new string[5];
InBlock.gif            
//文件的创建时间和访问时间。
InBlock.gif
            int iCount=0;    int iconIndex=1;//用1,而不用0是要让过0号图标。
InBlock.gif
            foreach(FileInfo fileInfo in dirFiles)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strFileName=fileInfo.Name;           
InBlock.gif            
InBlock.gif                
//如果不是文件pagefile.sys
InBlock.gif
                if(! strFileName.Equals("pagefile.sys"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    arrSubItem[
0]=strFileName;
InBlock.gif                    arrSubItem[
1]=fileInfo.Length+" 字节";
InBlock.gif                    arrSubItem[
2]=fileInfo.CreationTime.ToString();
InBlock.gif                    arrSubItem[
3]=fileInfo.LastAccessTime.ToString();
InBlock.gif                    arrSubItem[
4]=GetTypeName(strFileName);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
InBlock.gif   
InBlock.gif
InBlock.gif                
//得到每个文件的图标
InBlock.gif
                string str=fileInfo.FullName;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SetIcon(LisrimageList,str,
false);
InBlock.gif                    SetIcon(LisrimageList2,str,
true);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
InBlock.gif        
InBlock.gif
InBlock.gif                
//插入列表项    
InBlock.gif
                ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
InBlock.gif                listView1.Items.Insert(iCount,LiItem);
InBlock.gif
InBlock.gif                iCount
++
InBlock.gif                iconIndex
++;//必须加在listView1.Items.Insert(iCount,LiItem);
ExpandedSubBlockEnd.gif
            }

InBlock.gif            strFilePath
=strName;//把路径赋值于全局变量strFilePath
InBlock.gif

InBlock.gif            textBox1.Text
=strName;
InBlock.gif            
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"";
InBlock.gif            
this.Cursor=Cursors.Arrow;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
InBlock.gif
            int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
InBlock.gif

InBlock.gif            DirectoryInfo Dir
=new DirectoryInfo(strName);//创建目录对象。
InBlock.gif
            foreach(DirectoryInfo di in Dir.GetDirectories())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   
InBlock.gif                ListViewItem LiItem
=new ListViewItem(di.Name,0);
InBlock.gif                listView1.Items.Insert(iItem,LiItem);
InBlock.gif                iItem
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }
 
None.gif
None.gif  
// *************************************************************************************
None.gif
 
None.gif 
private   void  listView1_ItemActivate( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(listView1.FocusedItem.SubItems.Count>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif    System.Diagnostics.Process.Start(str); 
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ InitList2(str); }
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
catch  dot.gifreturn;}
ExpandedBlockEnd.gif  }

None.gif  
// *************************************************************************************
None.gif
private   string  GetTypeName( string  fileName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  MyFileInfomation fileInfo 
= new MyFileInfomation();//初始化MyFileInfomation结构
InBlock.gif  
InBlock.gif  
//调用GetFileInfo函数,最后一个参数说明获取的是文件类型(SHGFI_TYPENAME)
InBlock.gif
  int res = GetFileInfo(fileName, (int)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
InBlock.gif    
ref fileInfo, Marshal.SizeOf(fileInfo), (int)GetFileInfoFlags.SHGFI_TYPENAME);
InBlock.gif
InBlock.gif  
return fileInfo.szTypeName;
ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/flashicp/archive/2007/04/02/696664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值