1
//C#资源管理器源码
2
using System;
3
using System.Drawing;
4
using System.Collections;
5
using System.ComponentModel;
6
using System.Windows.Forms;
7
using System.Data;
8
using System.IO;
9
using System.Runtime.InteropServices;
10![]()
11
namespace myTree
12
{
13
/// <summary>
14
/// Form1 的摘要说明。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.ImageList TreeImageList;
19
private System.Windows.Forms.MainMenu mainMenu1;
20
private System.Windows.Forms.MenuItem menuItem1;
21
private System.Windows.Forms.ListView listView1;
22
private System.Windows.Forms.TextBox textBox1;
23
private System.Windows.Forms.TreeView treeView1;
24
private System.Windows.Forms.Panel panel2;
25
private System.Windows.Forms.Panel panel1;
26
private System.Windows.Forms.Splitter splitter1;
27
private System.Windows.Forms.Button button1;
28
private System.Windows.Forms.ImageList LisrimageList;
29
private System.Windows.Forms.ImageList LisrimageList2;
30
private System.Windows.Forms.StatusBar statusBar1;
31
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
32
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
33
private System.Windows.Forms.MenuItem menuItem2;
34
private System.Windows.Forms.MenuItem menuItem3;
35
private System.Windows.Forms.MenuItem menuItem4;
36
private System.Windows.Forms.MenuItem menuItem5;
37
private System.Windows.Forms.MenuItem menuItem6;
38
private System.Windows.Forms.MenuItem menuItem7;
39
private System.Windows.Forms.Label label1;
40
private System.ComponentModel.IContainer components;
41![]()
42
public Form1()
43
{
44
//
45
// Windows 窗体设计器支持所必需的
46
//
47
InitializeComponent();
48![]()
49
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",15);
50
TreeImageList.Images.Add(ic0);
51
Icon ic1=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",5);
52
TreeImageList.Images.Add(ic1);
53
Icon ic2=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",7);
54
TreeImageList.Images.Add(ic2);
55
Icon ic3=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",11);
56
TreeImageList.Images.Add(ic3);
57![]()
58
Icon ic4=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
59
TreeImageList.Images.Add(ic4);
60
Icon ic5=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",4);
61
TreeImageList.Images.Add(ic5);
62
Icon ic6=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",101);
63
TreeImageList.Images.Add(ic6);
64![]()
65![]()
66
GetDrive();
67
}
68![]()
69
/// <summary>
70
/// 清理所有正在使用的资源。
71
/// </summary>
72
protected override void Dispose( bool disposing )
73
{
74
if( disposing )
75
{
76
if (components != null)
77
{
78
components.Dispose();
79
}
80
}
81
base.Dispose( disposing );
82
}
83![]()
84
Windows Form Designer generated code
319![]()
320
/// <summary>
321
/// 应用程序的主入口点。
322
/// </summary>
323
[STAThread]
324
static void Main()
325
{
326
Application.Run(new Form1());
327
}
328![]()
329
//*************************************************************************************
330
[DllImport("Shell32.dll")]
331
public static extern int ExtractIcon(IntPtr h,string strx,int ii);
332![]()
333
[DllImport("Shell32.dll")]
334
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);
335![]()
336
public struct SHFILEINFO
337
{
338
public IntPtr hIcon;
339
public int iIcon;
340
public uint dwAttributes;
341
public char szDisplayName;
342
public char szTypeName;
343
}
344
345![]()
346
string strFilePath="";
347
//*************************************************************************************
348
349
protected virtual Icon myExtractIcon(string FileName,int iIndex)
350
{
351
try
352
{
353
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);
354
if(! hIcon.Equals(null))
355
{
356
Icon icon=Icon.FromHandle(hIcon);
357
return icon;
358
}
359
}
360
catch(Exception ex)
361
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
362
return null;
363
}
364
//*************************************************************************************
365![]()
366
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)
367
{
368
SHFILEINFO fi=new SHFILEINFO();
369
if(tf==true)
370
{
371
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
372
try
373
{
374
if(iTotal >0)
375
{
376
Icon ic=Icon.FromHandle(fi.hIcon);
377
imageList.Images.Add(ic);
378
//return ic;
379
}
380
}
381
catch(Exception ex)
382
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
383
}
384
else
385
{
386
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
387
try
388
{
389
if(iTotal >0)
390
{
391
Icon ic=Icon.FromHandle(fi.hIcon);
392
imageList.Images.Add(ic);
393
//return ic;
394
}
395
}
396
catch(Exception ex)
397
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
398
}
399
// return null;
400
}
401
//*************************************************************************************
402![]()
403
public void GetDrive()
404
{
405
treeView1.ImageList=TreeImageList;
406
407
treeView1.BeginUpdate();
408
treeView1.Nodes.Clear();
409![]()
410
TreeNode RootNode=new TreeNode("我的电脑",0,0);
411
treeView1.Nodes.Add(RootNode);
412
413
int iImageIndex=2; int iSelectedIndex=2;
414
string[] astrDrives = Directory.GetLogicalDrives();
415![]()
416
foreach (string str in astrDrives)
417
{
418
if(str == "A:\\")
419
{ iImageIndex=1; iSelectedIndex=1;}
420
else if(str == "G:\\")
421
{ iImageIndex=3; iSelectedIndex=3;}
422
else
423
{ iImageIndex=2; iSelectedIndex=2;}
424![]()
425
TreeNode tnDrive = new TreeNode(str, iImageIndex,iSelectedIndex);
426
treeView1.Nodes[0].Nodes.Add(tnDrive);
427
AddDirectories(tnDrive);
428![]()
429
if(str == "C:\\")
430
{ treeView1.SelectedNode=tnDrive;}
431
}
432
treeView1.EndUpdate();
433
}
434
//*************************************************************************************
435![]()
436
void AddDirectories(TreeNode tn)
437
{
438
tn.Nodes.Clear();
439![]()
440
string strPath=tn.FullPath;
441
strPath=strPath.Remove(0,5);
442![]()
443
//获得当前目录
444
DirectoryInfo dirinfo = new DirectoryInfo(strPath);
445
DirectoryInfo[] adirinfo;
446
try
447
{
448
adirinfo = dirinfo.GetDirectories();
449
}
450
catch
451
{ return;}
452![]()
453
int iImageIndex=4; int iSelectedIndex=5;
454
foreach (DirectoryInfo di in adirinfo)
455
{
456
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")
457
{iImageIndex=6; iSelectedIndex=6;}
458
else
459
{iImageIndex=4; iSelectedIndex=5;}
460![]()
461
TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
462
tn.Nodes.Add(tnDir);
463
}
464![]()
465![]()
466
/*
467
//获得当前目录下的所有文件
468
FileInfo[] dirFiles;
469
dirFiles=dirinfo.GetFiles();
470
int iCount=7;
471![]()
472
foreach (FileInfo fi in dirFiles)
473
{
474
//得到每个文件的图标
475
string str=fi.FullName;
476
try
477
{
478
SetIcon(TreeImageList,str,false);
479
}
480
catch(Exception ex)
481
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
482
483
TreeNode tnDir = new TreeNode(fi.Name, iCount, iCount);
484
tn.Nodes.Add(tnDir);
485![]()
486
iCount++;
487
}
488
*/
489
}
490
//*************************************************************************************
491
//在大图标、小图标、详细列表切换时调用
492
protected virtual void ListViewAB(int iii)
493
{
494
if(iii==1)
495
{
496
LisrimageList2.ImageSize=new Size(32,32);
497
listView1.LargeImageList=LisrimageList2;
498
499
if(listView1.View==View.Details||listView1.View==View.SmallIcon)
500
{listView1.View=View.LargeIcon;}
501
}
502
else if(iii==2)
503
{
504
if(listView1.View==View.Details||listView1.View==View.LargeIcon)
505
{listView1.View=View.SmallIcon;}
506
}
507
else
508
{
509
if(listView1.View==View.LargeIcon||listView1.View==View.SmallIcon)
510
{listView1.View=View.Details;}
511
}
512
}
513
//*************************************************************************************
514
private void treeView1_BeforeExpand_1(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
515
{
516
//base.OnBeforeExpand(e);
517![]()
518
treeView1.BeginUpdate();
519![]()
520
foreach (TreeNode tn in e.Node.Nodes)
521
{ AddDirectories(tn);}
522![]()
523
treeView1.EndUpdate();
524
}
525![]()
526
//*************************************************************************************
527![]()
528
protected virtual void InitList(TreeNode tn)
529
{
530
this.statusBarPanel1.Text="正在刷新文件夹,请稍等
..";
531
this.Cursor=Cursors.WaitCursor;
532![]()
533
this.LisrimageList2.Images.Clear();
534
this.LisrimageList.Images.Clear();
535
listView1.SmallImageList=LisrimageList;
536
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
537
LisrimageList.Images.Add(ic0);。
538
LisrimageList2.Images.Add(ic0);
539![]()
540
listView1.Clear();
541
//设置列表框的表头
542
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
543
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
544
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
545
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
546![]()
547
548
string strPath=tn.FullPath;
549
strPath=strPath.Remove(0,5);
550
//获得当前目录下的所有文件
551
DirectoryInfo curDir=new DirectoryInfo(strPath);//创建目录对象。
552
FileInfo[] dirFiles;
553
try
554
{
555
dirFiles=curDir.GetFiles();
556
}
557
catch { return;}
558![]()
559
string []arrSubItem=new string[4];
560
//文件的创建时间和访问时间。
561
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
562
foreach(FileInfo fileInfo in dirFiles)
563
{
564
string strFileName=fileInfo.Name;
565
566
//如果不是文件pagefile.sys
567
if(! strFileName.Equals("pagefile.sys"))
568
{
569
arrSubItem[0]=strFileName;
570
arrSubItem[1]=fileInfo.Length+" 字节";
571
arrSubItem[2]=fileInfo.CreationTime.ToString();
572
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
573
}
574
else
575
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
576
577![]()
578
//得到每个文件的图标
579
string str=fileInfo.FullName;
580
try
581
{
582
SetIcon(LisrimageList,str,false);
583
SetIcon(LisrimageList2,str,true);
584
}
585
catch(Exception ex)
586
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
587
588![]()
589
//插入列表项
590
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
591
listView1.Items.Insert(iCount,LiItem);
592![]()
593
iCount++;
594
iconIndex++;
595
}
596
strFilePath=strPath;
597
textBox1.Text=strPath;
598
this.statusBarPanel1.Text=strPath;
599
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
600
this.Cursor=Cursors.Arrow;
601![]()
602
603![]()
604
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
605
int iItem=0;
606![]()
607
DirectoryInfo Dir=new DirectoryInfo(strPath);
608
foreach(DirectoryInfo di in Dir.GetDirectories())
609
{
610
ListViewItem LiItem=new ListViewItem(di.Name,0);
611
listView1.Items.Insert(iItem,LiItem);
612
iItem++;
613
}
614![]()
615
}
616
//*************************************************************************************
617![]()
618
protected virtual void InitList2(string strName)
619
{
620
this.statusBarPanel1.Text="正在刷新文件夹,请稍等
..";
621
this.Cursor=Cursors.WaitCursor;
622![]()
623
this.LisrimageList2.Images.Clear();
624
this.LisrimageList.Images.Clear();
625
listView1.SmallImageList=LisrimageList;
626
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
627
LisrimageList.Images.Add(ic0);
628
LisrimageList2.Images.Add(ic0);
629![]()
630
listView1.Clear();
631
//设置列表框的表头
632
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
633
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
634
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
635
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
636![]()
637
638
//获得当前目录下的所有文件
639
DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。
640
FileInfo[] dirFiles;
641
try
642
{
643
dirFiles=curDir.GetFiles();
644
}
645
catch { return;}
646![]()
647
string []arrSubItem=new string[4];
648
//文件的创建时间和访问时间。
649
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
650
foreach(FileInfo fileInfo in dirFiles)
651
{
652
string strFileName=fileInfo.Name;
653
654
//如果不是文件pagefile.sys
655
if(! strFileName.Equals("pagefile.sys"))
656
{
657
arrSubItem[0]=strFileName;
658
arrSubItem[1]=fileInfo.Length+" 字节";
659
arrSubItem[2]=fileInfo.CreationTime.ToString();
660
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
661
}
662
else
663
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
664
665![]()
666
//得到每个文件的图标
667
string str=fileInfo.FullName;
668
try
669
{
670
SetIcon(LisrimageList,str,false);
671
SetIcon(LisrimageList2,str,true);
672
}
673
catch(Exception ex)
674
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
675
676![]()
677
//插入列表项
678
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
679
listView1.Items.Insert(iCount,LiItem);
680![]()
681
iCount++;
682
iconIndex++;//必须加在listView1.Items.Insert(iCount,LiItem);
683
}
684
strFilePath=strName;//把路径赋值于全局变量strFilePath
685![]()
686
textBox1.Text=strName;
687
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
688
this.Cursor=Cursors.Arrow;
689![]()
690
691![]()
692
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
693
int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
694![]()
695
DirectoryInfo Dir=new DirectoryInfo(strName);//创建目录对象。
696
foreach(DirectoryInfo di in Dir.GetDirectories())
697
{
698
ListViewItem LiItem=new ListViewItem(di.Name,0);
699
listView1.Items.Insert(iItem,LiItem);
700
iItem++;
701
}
702![]()
703
}
704
//*************************************************************************************
705![]()
706
private void treeView1_AfterSelect_1(object sender, System.Windows.Forms.TreeViewEventArgs e)
707
{
708
if(e.Node.Text=="我的电脑")
709
{ return;}
710![]()
711
InitList(e.Node);
712
}
713
//*************************************************************************************
714![]()
715
private void button1_Click(object sender, System.EventArgs e)
716
{
717
this.panel1.Hide();
718
}
719
//*************************************************************************************
720![]()
721
private void listView1_ItemActivate(object sender, System.EventArgs e)
722
{
723
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);
724
try
725
{
726
if(listView1.FocusedItem.SubItems.Count>1)
727
{ System.Diagnostics.Process.Start(str); }
728
else
729
{ InitList2(str); }
730
}
731
catch { return;}
732
}
733
//*************************************************************************************
734
private void menuItem3_Click(object sender, System.EventArgs e)
735
{
736
this.panel1.Visible=! panel1.Visible;
737
}
738
//*************************************************************************************
739
private void menuItem5_Click(object sender, System.EventArgs e)
740
{
741
ListViewAB(1);
742
}
743
//*************************************************************************************
744
private void menuItem6_Click(object sender, System.EventArgs e)
745
{
746
ListViewAB(2);
747
}
748
//*************************************************************************************
749
private void menuItem7_Click(object sender, System.EventArgs e)
750
{
751
ListViewAB(3);
752
}
753
//*************************************************************************************
754![]()
755
}
756
}
757![]()
758![]()
//C#资源管理器源码2
using System;3
using System.Drawing;4
using System.Collections;5
using System.ComponentModel;6
using System.Windows.Forms;7
using System.Data;8
using System.IO;9
using System.Runtime.InteropServices;10

11
namespace myTree12
{13
/// <summary>14
/// Form1 的摘要说明。15
/// </summary>16
public class Form1 : System.Windows.Forms.Form17
{18
private System.Windows.Forms.ImageList TreeImageList;19
private System.Windows.Forms.MainMenu mainMenu1;20
private System.Windows.Forms.MenuItem menuItem1;21
private System.Windows.Forms.ListView listView1;22
private System.Windows.Forms.TextBox textBox1;23
private System.Windows.Forms.TreeView treeView1;24
private System.Windows.Forms.Panel panel2;25
private System.Windows.Forms.Panel panel1;26
private System.Windows.Forms.Splitter splitter1;27
private System.Windows.Forms.Button button1;28
private System.Windows.Forms.ImageList LisrimageList;29
private System.Windows.Forms.ImageList LisrimageList2;30
private System.Windows.Forms.StatusBar statusBar1;31
private System.Windows.Forms.StatusBarPanel statusBarPanel1;32
private System.Windows.Forms.StatusBarPanel statusBarPanel2;33
private System.Windows.Forms.MenuItem menuItem2;34
private System.Windows.Forms.MenuItem menuItem3;35
private System.Windows.Forms.MenuItem menuItem4;36
private System.Windows.Forms.MenuItem menuItem5;37
private System.Windows.Forms.MenuItem menuItem6;38
private System.Windows.Forms.MenuItem menuItem7;39
private System.Windows.Forms.Label label1;40
private System.ComponentModel.IContainer components;41

42
public Form1()43
{44
//45
// Windows 窗体设计器支持所必需的46
//47
InitializeComponent();48

49
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",15);50
TreeImageList.Images.Add(ic0);51
Icon ic1=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",5);52
TreeImageList.Images.Add(ic1);53
Icon ic2=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",7);54
TreeImageList.Images.Add(ic2);55
Icon ic3=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",11);56
TreeImageList.Images.Add(ic3);57

58
Icon ic4=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);59
TreeImageList.Images.Add(ic4);60
Icon ic5=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",4);61
TreeImageList.Images.Add(ic5);62
Icon ic6=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",101);63
TreeImageList.Images.Add(ic6);64

65

66
GetDrive();67
}68

69
/// <summary>70
/// 清理所有正在使用的资源。71
/// </summary>72
protected override void Dispose( bool disposing )73
{74
if( disposing )75
{76
if (components != null) 77
{78
components.Dispose();79
}80
}81
base.Dispose( disposing );82
}83

84
Windows Form Designer generated code319

320
/// <summary>321
/// 应用程序的主入口点。322
/// </summary>323
[STAThread]324
static void Main() 325
{326
Application.Run(new Form1());327
}328

329
//*************************************************************************************330
[DllImport("Shell32.dll")] 331
public static extern int ExtractIcon(IntPtr h,string strx,int ii);332

333
[DllImport("Shell32.dll")] 334
public static extern int SHGetFileInfo(string pszPath,uint dwFileAttributes,ref SHFILEINFO psfi,uint cbFileInfo, uint uFlags);335

336
public struct SHFILEINFO337
{ 338
public IntPtr hIcon; 339
public int iIcon; 340
public uint dwAttributes;341
public char szDisplayName; 342
public char szTypeName; 343
}344
345

346
string strFilePath="";347
//************************************************************************************* 348
349
protected virtual Icon myExtractIcon(string FileName,int iIndex)350
{351
try352
{353
IntPtr hIcon=(IntPtr)ExtractIcon(this.Handle,FileName,iIndex);354
if(! hIcon.Equals(null))355
{356
Icon icon=Icon.FromHandle(hIcon);357
return icon;358
}359
}360
catch(Exception ex)361
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 362
return null;363
}364
//*************************************************************************************365

366
protected virtual void SetIcon(ImageList imageList,string FileName,bool tf)367
{368
SHFILEINFO fi=new SHFILEINFO();369
if(tf==true)370
{371
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 16640);//SHGFI_ICON|SHGFI_SMALLICON372
try373
{374
if(iTotal >0)375
{376
Icon ic=Icon.FromHandle(fi.hIcon);377
imageList.Images.Add(ic);378
//return ic;379
}380
}381
catch(Exception ex)382
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 383
}384
else385
{386
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);387
try388
{389
if(iTotal >0)390
{391
Icon ic=Icon.FromHandle(fi.hIcon);392
imageList.Images.Add(ic);393
//return ic;394
}395
}396
catch(Exception ex)397
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);} 398
}399
// return null;400
}401
//*************************************************************************************402

403
public void GetDrive()404
{405
treeView1.ImageList=TreeImageList;406
407
treeView1.BeginUpdate();408
treeView1.Nodes.Clear();409

410
TreeNode RootNode=new TreeNode("我的电脑",0,0);411
treeView1.Nodes.Add(RootNode);412
413
int iImageIndex=2; int iSelectedIndex=2;414
string[] astrDrives = Directory.GetLogicalDrives();415

416
foreach (string str in astrDrives)417
{418
if(str == "A:\\")419
{ iImageIndex=1; iSelectedIndex=1;}420
else if(str == "G:\\")421
{ iImageIndex=3; iSelectedIndex=3;}422
else 423
{ iImageIndex=2; iSelectedIndex=2;}424

425
TreeNode tnDrive = new TreeNode(str, iImageIndex,iSelectedIndex);426
treeView1.Nodes[0].Nodes.Add(tnDrive);427
AddDirectories(tnDrive);428

429
if(str == "C:\\")430
{ treeView1.SelectedNode=tnDrive;}431
}432
treeView1.EndUpdate();433
}434
//*************************************************************************************435

436
void AddDirectories(TreeNode tn)437
{438
tn.Nodes.Clear();439

440
string strPath=tn.FullPath;441
strPath=strPath.Remove(0,5);442

443
//获得当前目录444
DirectoryInfo dirinfo = new DirectoryInfo(strPath);445
DirectoryInfo[] adirinfo;446
try447
{448
adirinfo = dirinfo.GetDirectories();449
}450
catch451
{ return;}452

453
int iImageIndex=4; int iSelectedIndex=5;454
foreach (DirectoryInfo di in adirinfo)455
{456
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")457
{iImageIndex=6; iSelectedIndex=6;}458
else 459
{iImageIndex=4; iSelectedIndex=5;}460

461
TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);462
tn.Nodes.Add(tnDir);463
}464

465

466
/*467
//获得当前目录下的所有文件 468
FileInfo[] dirFiles;469
dirFiles=dirinfo.GetFiles();470
int iCount=7;471

472
foreach (FileInfo fi in dirFiles)473
{474
//得到每个文件的图标475
string str=fi.FullName;476
try477
{478
SetIcon(TreeImageList,str,false);479
}480
catch(Exception ex)481
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}482
483
TreeNode tnDir = new TreeNode(fi.Name, iCount, iCount);484
tn.Nodes.Add(tnDir);485

486
iCount++;487
}488
*/489
}490
//*************************************************************************************491
//在大图标、小图标、详细列表切换时调用492
protected virtual void ListViewAB(int iii)493
{494
if(iii==1)495
{496
LisrimageList2.ImageSize=new Size(32,32);497
listView1.LargeImageList=LisrimageList2;498
499
if(listView1.View==View.Details||listView1.View==View.SmallIcon)500
{listView1.View=View.LargeIcon;}501
}502
else if(iii==2)503
{504
if(listView1.View==View.Details||listView1.View==View.LargeIcon)505
{listView1.View=View.SmallIcon;}506
}507
else508
{509
if(listView1.View==View.LargeIcon||listView1.View==View.SmallIcon)510
{listView1.View=View.Details;}511
}512
}513
//*************************************************************************************514
private void treeView1_BeforeExpand_1(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)515
{516
//base.OnBeforeExpand(e);517

518
treeView1.BeginUpdate();519

520
foreach (TreeNode tn in e.Node.Nodes)521
{ AddDirectories(tn);}522

523
treeView1.EndUpdate();524
}525

526
//*************************************************************************************527

528
protected virtual void InitList(TreeNode tn)529
{530
this.statusBarPanel1.Text="正在刷新文件夹,请稍等
..";531
this.Cursor=Cursors.WaitCursor;532

533
this.LisrimageList2.Images.Clear();534
this.LisrimageList.Images.Clear();535
listView1.SmallImageList=LisrimageList;536
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);537
LisrimageList.Images.Add(ic0);。538
LisrimageList2.Images.Add(ic0);539

540
listView1.Clear();541
//设置列表框的表头542
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);543
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);544
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);545
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);546

547
548
string strPath=tn.FullPath;549
strPath=strPath.Remove(0,5);550
//获得当前目录下的所有文件 551
DirectoryInfo curDir=new DirectoryInfo(strPath);//创建目录对象。552
FileInfo[] dirFiles;553
try554
{555
dirFiles=curDir.GetFiles();556
}557
catch { return;}558

559
string []arrSubItem=new string[4];560
//文件的创建时间和访问时间。561
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。562
foreach(FileInfo fileInfo in dirFiles)563
{564
string strFileName=fileInfo.Name; 565
566
//如果不是文件pagefile.sys567
if(! strFileName.Equals("pagefile.sys"))568
{569
arrSubItem[0]=strFileName;570
arrSubItem[1]=fileInfo.Length+" 字节";571
arrSubItem[2]=fileInfo.CreationTime.ToString();572
arrSubItem[3]=fileInfo.LastAccessTime.ToString();573
}574
else575
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}576
577

578
//得到每个文件的图标579
string str=fileInfo.FullName;580
try581
{582
SetIcon(LisrimageList,str,false);583
SetIcon(LisrimageList2,str,true);584
}585
catch(Exception ex)586
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}587
588

589
//插入列表项 590
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);591
listView1.Items.Insert(iCount,LiItem);592

593
iCount++; 594
iconIndex++;595
}596
strFilePath=strPath;597
textBox1.Text=strPath;598
this.statusBarPanel1.Text=strPath;599
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";600
this.Cursor=Cursors.Arrow;601

602
603

604
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。605
int iItem=0;606

607
DirectoryInfo Dir=new DirectoryInfo(strPath);608
foreach(DirectoryInfo di in Dir.GetDirectories())609
{ 610
ListViewItem LiItem=new ListViewItem(di.Name,0);611
listView1.Items.Insert(iItem,LiItem);612
iItem++;613
}614

615
} 616
//*************************************************************************************617

618
protected virtual void InitList2(string strName)619
{620
this.statusBarPanel1.Text="正在刷新文件夹,请稍等
..";621
this.Cursor=Cursors.WaitCursor;622

623
this.LisrimageList2.Images.Clear();624
this.LisrimageList.Images.Clear();625
listView1.SmallImageList=LisrimageList;626
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);627
LisrimageList.Images.Add(ic0);628
LisrimageList2.Images.Add(ic0);629

630
listView1.Clear();631
//设置列表框的表头632
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);633
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);634
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);635
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);636

637
638
//获得当前目录下的所有文件 639
DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。640
FileInfo[] dirFiles;641
try642
{643
dirFiles=curDir.GetFiles();644
}645
catch { return;}646

647
string []arrSubItem=new string[4];648
//文件的创建时间和访问时间。649
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。650
foreach(FileInfo fileInfo in dirFiles)651
{652
string strFileName=fileInfo.Name; 653
654
//如果不是文件pagefile.sys655
if(! strFileName.Equals("pagefile.sys"))656
{657
arrSubItem[0]=strFileName;658
arrSubItem[1]=fileInfo.Length+" 字节";659
arrSubItem[2]=fileInfo.CreationTime.ToString();660
arrSubItem[3]=fileInfo.LastAccessTime.ToString();661
}662
else663
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}664
665

666
//得到每个文件的图标667
string str=fileInfo.FullName;668
try669
{670
SetIcon(LisrimageList,str,false);671
SetIcon(LisrimageList2,str,true);672
}673
catch(Exception ex)674
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}675
676

677
//插入列表项 678
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);679
listView1.Items.Insert(iCount,LiItem);680

681
iCount++; 682
iconIndex++;//必须加在listView1.Items.Insert(iCount,LiItem);683
}684
strFilePath=strName;//把路径赋值于全局变量strFilePath685

686
textBox1.Text=strName;687
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";688
this.Cursor=Cursors.Arrow;689

690
691

692
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。693
int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。694

695
DirectoryInfo Dir=new DirectoryInfo(strName);//创建目录对象。696
foreach(DirectoryInfo di in Dir.GetDirectories())697
{ 698
ListViewItem LiItem=new ListViewItem(di.Name,0);699
listView1.Items.Insert(iItem,LiItem);700
iItem++;701
}702

703
} 704
//*************************************************************************************705

706
private void treeView1_AfterSelect_1(object sender, System.Windows.Forms.TreeViewEventArgs e)707
{708
if(e.Node.Text=="我的电脑") 709
{ return;}710

711
InitList(e.Node);712
}713
//*************************************************************************************714

715
private void button1_Click(object sender, System.EventArgs e)716
{717
this.panel1.Hide();718
}719
//*************************************************************************************720

721
private void listView1_ItemActivate(object sender, System.EventArgs e)722
{723
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);724
try725
{726
if(listView1.FocusedItem.SubItems.Count>1)727
{ System.Diagnostics.Process.Start(str); }728
else729
{ InitList2(str); }730
}731
catch { return;}732
}733
//*************************************************************************************734
private void menuItem3_Click(object sender, System.EventArgs e)735
{736
this.panel1.Visible=! panel1.Visible;737
}738
//*************************************************************************************739
private void menuItem5_Click(object sender, System.EventArgs e)740
{741
ListViewAB(1);742
}743
//*************************************************************************************744
private void menuItem6_Click(object sender, System.EventArgs e)745
{746
ListViewAB(2);747
}748
//*************************************************************************************749
private void menuItem7_Click(object sender, System.EventArgs e)750
{751
ListViewAB(3);752
}753
//*************************************************************************************754

755
}756
}757

758


浙公网安备 33010602011771号