在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#非常强大的资源管理器(源码)

C#非常强大的资源管理器(源码)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.33M
  • 下载次数:275
  • 浏览次数:4401
  • 发布时间:2018-02-11
  • 实例类别:C#语言基础
  • 发 布 人:bbspc
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# c 源码 管理

实例介绍

【实例简介】C#非常强大的资源管理器(源码),用户自定义控件实现

【实例截图】

from clipboard


from clipboard


from clipboard

【核心代码】


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsControl
{
    public partial class ExplorerControl : UserControl
    {
        #region API函数
        [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
        protected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
        #endregion
 
        #region 公共变量
        /// <summary>
        /// 本地当前路径
        /// </summary>
        public string FilePathStr = "";
         
        /// <summary>
        /// 所有磁盘信息
        /// </summary>
        public DriveInfo[] Drivers = DriveInfo.GetDrives();
        /// <summary>
        /// 服务器当前选中的路径
        /// </summary>
        public string SitPath = "/";
        /// <summary>
        /// 服务器名或IP地址
        /// </summary>
        public string ServerName = "127.0.0.1";
        /// <summary>
        /// 登陆服务器用户名
        /// </summary>
        public string ServerUserName = "";
        /// <summary>
        /// 登陆服务器密码
        /// </summary>
        public string ServerPasswordName = "127.0.0.1";
        /// <summary>
        /// 登陆服务器端口
        /// </summary>
        public int ServerPort = 21;
        #endregion
 
        #region 资源管理器构造函数
        public ExplorerControl()
        {
            InitializeComponent();
            CreatTreeview();
        }
        #endregion
 
        #region 树目录和listview更新方法
        /// <summary>
        /// 初始化树目录
        /// </summary>
        #region
        public void CreatTreeview()
        {
            //禁止任何树视图重绘
            ExplorerTree.BeginUpdate();
            //删除所有ExplorerTree中节点
            ExplorerTree.Nodes.Clear();
 
            TreeNode rootNode = new TreeNode();
            //"我的电脑", 0, 0
            rootNode.Text = string.Format("我的电脑");
            rootNode.ImageIndex = 2;
            rootNode.SelectedImageIndex = 2;
            ExplorerTree.Nodes.Add(rootNode);
 
            //返回当前计算机中逻辑驱动器名称的数组
 
            //
            #region//遍历驱动器名称,重画TreeView,按照windows习惯显示
            //但是要用tn.Tag属性记录其绝对路径,以便后面操作(遍历文件夹下文件)
            int j = 0;
            foreach (DriveInfo driver in Drivers)
            {
                TreeNode tn = new TreeNode(driver.Name);
                tn.Tag = driver.RootDirectory.FullName;
                ExplorerTree.Nodes[0].Nodes.Add(tn);
                Icon fileIcon = GetSystemIcon.GetIcon(driver.Name, false);
                TVimageList.Images.Add(driver.Name, fileIcon);
                tn.ImageKey = driver.Name;
                tn.SelectedImageKey = driver.Name;
                AddDirectories(tn);
 
                if (driver.DriveType == DriveType.CDRom)
                {
                    ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("DVD RW 驱动器("   driver.Name.Remove(2)   ")");
 
                }
                else if (driver.DriveType == DriveType.Removable)
                {
                    ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("可移动磁盘("   driver.Name.Remove(2)   ")");
                }
                else if (driver.DriveType == DriveType.Fixed)
                {
                    ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("本地磁盘("   driver.Name.Remove(2)   ")");
 
                }
                else
                {
                    ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("UnknowDriver("   driver.Name.Remove(2)   ")");
 
                }
                j  ;
 
 
            }
            #endregion
            //
            ExplorerTree.SelectedNode = rootNode.FirstNode;
            //启用视图重绘
            ExplorerTree.EndUpdate();
        }
        #endregion
 
        /// <summary>
        /// 在ListView中列出各个磁盘详细信息
        /// </summary>
        #region
        private void InitDisksList()
        {
            listView磁盘文件.BeginUpdate();
            listView磁盘文件.Clear();
 
            #region//设置关于磁盘的ListView标题块
            //设置默认显示方式
            //listView磁盘文件.View = View.Tile;
 
            listView磁盘文件.Columns.Add("名称", 160, HorizontalAlignment.Left);
            listView磁盘文件.Columns.Add("类型", 120, HorizontalAlignment.Left);
            listView磁盘文件.Columns.Add("总大小", 120, HorizontalAlignment.Left);
            listView磁盘文件.Columns.Add("剩余空间", 200, HorizontalAlignment.Left);
            #endregion
 
            #region //向ListView中添加磁盘信息
            foreach (DriveInfo Driver in Drivers)
            {
                ListViewItem fileItem = listView磁盘文件.Items.Add("本地磁盘("   Driver.Name.Remove(2)   ")");
                Icon fileIcon = GetSystemIcon.GetIcon(Driver.Name, false);
 
                this.TVimageList.Images.Add(fileItem.Text, fileIcon);
 
                this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon);
                this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon);
                fileItem.ImageKey = fileItem.Text;
                fileItem.Name = Driver.RootDirectory.FullName;
                if (Driver.DriveType == DriveType.Fixed)
                {
                    fileItem.SubItems.Add("本地磁盘");
 
                }
                else if (Driver.DriveType == DriveType.Removable)
                {
                    fileItem.Text = "可移动磁盘("   Driver.Name.Remove(2)   ")";
                    fileItem.SubItems.Add("可移动磁盘");
                    fileIcon = GetSystemIcon.GetIcon(Driver.Name, false);
                    this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon);
                    this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon);
                    fileItem.ImageKey = fileItem.Text;
                }
                else if (Driver.DriveType == DriveType.CDRom)
                {
                    fileItem.Text = "CDRom驱动器("   Driver.Name.Remove(2)   ")";
                    fileItem.SubItems.Add("CDRom驱动器");
                    fileIcon = GetSystemIcon.GetIcon(Driver.Name, false);
                    this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon);
                    this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon);
 
                    fileItem.ImageKey = fileItem.Text;
                }
                else
                {
                    fileIcon = GetSystemIcon.GetIcon(Driver.Name, false);
                    this.TVimageList.Images.Add(fileItem.Text, fileIcon);
                    this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon);
                    fileItem.ImageKey = fileItem.Text;
                }
                if (Driver.IsReady)
                {
                    double G = 1000000000.00D;
                    //fileItem.SubItems.Add(Math.Round(Driver.TotalSize / G, 2)   "G");
                    //fileItem.SubItems.Add(Math.Round(Driver.TotalFreeSpace / G, 2)   "G");
                    fileItem.SubItems.Add(FileSize(Driver.TotalSize) );
                    fileItem.SubItems.Add(FileSize(Driver.TotalFreeSpace));
                    fileItem.Tag = Driver.TotalSize;
                }
                else if (Driver.DriveType == DriveType.CDRom)
                {
                    //listView1.Items.Clear();
                    this.ExplorerTree.Focus();
                }
                else
                {
                    MessageBox.Show("驱动器未就绪,请检查设备!", "温柔提醒", MessageBoxButtons.OK);
 
                }
 
            }
            #endregion
            listView磁盘文件.EndUpdate();
            FilePathStr = "我的电脑";
            TxtAddress.Text = FilePathStr;
        }
        #endregion
 
        /// <summary>
        /// 为tn节点添加子节点
        /// </summary>
        /// <param name="tn">TreeNode类型节点</param>
        #region
        private void AddDirectories(TreeNode tn)
        {
            tn.Nodes.Clear();
            //MessageBox.Show(StrPath);  
            DirectoryInfo dirInfo = new DirectoryInfo(tn.Tag.ToString());
            if (!dirInfo.Exists)
            {
                return;
            }
             
            DirectoryInfo[] dirs;
            try
            {
                //获得节点下的所有文件夹
                dirs = dirInfo.GetDirectories();
            }
            catch (Exception)
            {
                //throw e;
                //MessageBox.Show(e.Message);
                return;
            }
            //添加子节点
            foreach (DirectoryInfo dir in dirs)
            {
                if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                {
                    continue;
                }
                TreeNode sunTn = new TreeNode(dir.Name);
                sunTn.Tag = dir.FullName;
                tn.Nodes.Add(sunTn);
                //不能在这里用递归,速度太慢
                //AddDirectories(SubTn);
            }
        }
        #endregion
 
        /// <summary>
        /// TreeView节点将要展开的前画出其子节点,同时更新ListView
        /// </summary>
        /// <param name="sender">Object类型系统参数</param>
        /// <param name="e">TreeViewCancelEventArgs类型系统参数</param>
        #region
        private void ExplorerTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            //返回当前计算机中逻辑驱动器名称的数组(已经在程序开始处生命)
            //string[] Drivers = Environment.GetLogicalDrives();
            //这样重新设置所有的根节点不如只重新设置要展开的节点。
 
            if (e.Node.FullPath == "我的电脑")
            {
                InitDisksList();
                return;
            }
            TxtAddress.Text = e.Node.Tag.ToString();
            //禁止任何树视图重绘
            ExplorerTree.BeginUpdate();
            foreach (TreeNode tn in e.Node.Nodes)
            {
                AddDirectories(tn);
            }
            ExplorerTree.EndUpdate();
            //当打开某个节点的时候同时更新ListView1;
            ListUpdate(e.Node.Tag.ToString());
 
        }
        #endregion
 
        /// <summary>
        /// 更新ListView(即画出选定目录下的目录和文件)
        /// </summary>
        /// <param name="newPath">绝对路径</param>
 
        #region
 
        private void ListUpdate(string newPath)
        {
            if (File.Exists(newPath))
            {
                Process.Start(newPath);
            }
            else if (newPath == "我的电脑")
            {
                InitDisksList();
                FilePathStr = newPath;
            }
            else if (Directory.Exists(newPath))
            {
 
                #region//设置一般文件夹的ListView标题块
 
                listView磁盘文件.Columns.Clear();
                listView磁盘文件.Columns.Add("名称", 200, HorizontalAlignment.Left);
                listView磁盘文件.Columns.Add("修改时间", 115, HorizontalAlignment.Left);
                listView磁盘文件.Columns.Add("类型", 85, HorizontalAlignment.Left);
                listView磁盘文件.Columns.Add("大小", 80, HorizontalAlignment.Right);
 
                #endregion
 
                DirectoryInfo currentDir = new DirectoryInfo(newPath);
                DirectoryInfo[] dirs = new DirectoryInfo[0];
                FileInfo[] files = new FileInfo[0];
                dirs = currentDir.GetDirectories(); //获取目录
                files = currentDir.GetFiles(); //获取文件
 
                //删除ImageList中的程序图标
                foreach (ListViewItem item in listView磁盘文件.Items)
                {
                    if (item.Text.EndsWith(".exe")) //是程序
                    {
                        LVLargeimageList.Images.RemoveByKey(item.Text);
                        LVSmallimageList.Images.RemoveByKey(item.Text);
                    }
                }
                //
                listView磁盘文件.BeginUpdate();
                listView磁盘文件.Items.Clear();
                if (listView磁盘文件.View == View.Details)
                {
                    //添加上一层行
                    ListViewItem upItem = listView磁盘文件.Items.Add("上一层", "上一层", "上一层");
                    upItem.Name = "上一层";
                    upItem.SubItems.Add("");
                    upItem.SubItems.Add("");
                    upItem.SubItems.Add("");
                }
                //列出文件夹
                foreach (DirectoryInfo dir in dirs)
                {
                    //如果是系统文件和隐藏文件,则不添加到列表
                    if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        continue;
                    }
                    ListViewItem dirItem = listView磁盘文件.Items.Add(dir.Name);
                    //Icon fileIcon = GetSystemIcon.GetIcon(tn.Tag.ToString(), false);
                    //this.TVimageList.Images.Add(tn.Text, fileIcon);
                    //tn.ImageKey = tn.Text;
                    Icon fileIcon = GetSystemIcon.GetIconByFileType(dir.Name, false);
                    LVLargeimageList.Images.Add(dir.Name, fileIcon);
                    LVSmallimageList.Images.Add(dir.Name, fileIcon);
                    dirItem.ImageKey = dir.Name;
                    dirItem.Name = dir.FullName;
                    dirItem.SubItems.Add(dir.LastWriteTimeUtc.ToString("yyyy/MM/dd HH:mm"));
                    dirItem.SubItems.Add("文件夹");
                    dirItem.SubItems.Add("");
 
 
 
                }
                //列出文件
                foreach (FileInfo file in files)
                {
                    //如果是系统文件和隐藏文件,则不添加到列表
                    if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        continue;
                    }
                    ListViewItem fileItem = listView磁盘文件.Items.Add(file.Name);
                    if (file.Extension == ".exe" || file.Extension == "") //程序文件或无扩展名
                    {
                        Icon fileIcon = GetSystemIcon.GetIconByFileName(file.FullName);
                        LVLargeimageList.Images.Add(file.Name, fileIcon);
                        LVSmallimageList.Images.Add(file.Name, fileIcon);
                        fileItem.ImageKey = file.Name;
                    }
                    else //其它文件
                    {
                        if (!LVSmallimageList.Images.ContainsKey(file.Extension)) //ImageList中不存在此类图标
                        {
                            Icon fileIcon = GetSystemIcon.GetIconByFileName(file.FullName);
                            LVLargeimageList.Images.Add(file.Extension, fileIcon);
                            LVSmallimageList.Images.Add(file.Extension, fileIcon);
                        }
                        fileItem.ImageKey = file.Extension;
                    }
                    fileItem.Name = file.FullName;
                    fileItem.SubItems.Add(file.LastWriteTimeUtc.ToString("yyyy/MM/dd HH:mm"));
                    string filetype = file.Extension.Equals("")
                        ? "文件"
                        : file.Extension.Substring(file.Extension.IndexOf('.')   1).ToUpper();
                    fileItem.SubItems.Add(filetype.Equals("文件")
                        ? filetype
                        : (filetype.Equals("DLL") ? "应用程序扩展" : filetype   " 文件"));
                    fileItem.SubItems.Add(FileSize(file.Length));
 
                    fileItem.Tag = file.Length;
                }
                listView磁盘文件.EndUpdate();
                FilePathStr = newPath;
                TxtAddress.Text = newPath; //更新地址栏
                lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length,
                    listView磁盘文件.Items.Count);
                //DoInformation.Text = "本地浏览器";
                //   "个对象";     //更新状态栏
 
 
            }
        }
 
        #endregion
 
        public string FileSize(long size)
        {
            double newsize = double.Parse(size.ToString());
            string returnStr = newsize > 1024
                ? ((newsize/1024) > 1024
                    ? ((newsize / 1024 / 1024) > 1024 ? (((newsize / 1024 / 1024 / 1024).ToString("F")   " G")) : ((newsize / 1024 / 1024).ToString("F")   " M"))
                    : ((newsize / 1024).ToString("F")   " KB"))
                : (newsize   " B");
            return returnStr;
        }
 
        /// <summary>
        /// 选定某个节点后显示其下的文件夹和文件
        /// </summary>
        /// <param name="sender">object类型系统参数</param>
        /// <param name="e">TreeViewEventArgs类型系统参数</param>
        #region
        private void ExplorerTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text.Equals("我的电脑"))
            {
                InitDisksList();
            }
            else
            {
                TxtAddress.Text = e.Node.Tag.ToString();
                ListUpdate(TxtAddress.Text);
            }
        }
        #endregion
        #endregion
 
        #region 菜单按钮事件及方法
 
        private void Btn向上Click(object sender, EventArgs e)
        {
            try
            {
                int index = FilePathStr.LastIndexOf("\\");
                if (index > 2)
                {
                    string newpath = FilePathStr.Substring(0, index);
                    ListUpdate(newpath);
                }
                else
                {
                    InitDisksList();
                }
            }
            catch
            {
            }
        }
 
        //转到按钮事件
        private void Btn转到Click(object sender, EventArgs e)
        {
            ListUpdate(TxtAddress.Text);
        }
        private void Btn刷新Click(object sender, EventArgs e)
        {
            CreatTreeview();
        }
        //显示隐藏树目录按钮事件
        private void Btn树目录Click(object sender, EventArgs e)
        {
            ExplorerTree.Visible = Btn目录树.Checked;
        }
        private void Btn选定Click(object sender, EventArgs e)
        {
            ListViewItem[] items = new ListViewItem[listView磁盘文件 .SelectedItems.Count];
            for (int i = 0; i < items.Length; i  )
            {
                items[i] = listView磁盘文件.SelectedItems[i];
            }
            AddFile(items);
        }
        //地址文本框相应键盘回车事件
        private void AddressTxtKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                Btn转到Click(sender, null);
            }
        }
        //大图标显示按钮事件
        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //标题ToolStripMenuItem.CheckOnClick属性全部设为True;
            //即单击时选定,但是这个方法是在单击后执行所以不起作用
            //还要手动设置选中
            ClearCheckStatue();
            listView磁盘文件.View = View.LargeIcon;
            ListUpdate(FilePathStr);
            大图标ToolStripMenuItem.Checked = true;
            大图标ToolStripMenuItem1.Checked = true;
        }
        //小图标显示按钮事件
        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearCheckStatue();
            listView磁盘文件.View = View.SmallIcon;
            ListUpdate(FilePathStr);
            小图标ToolStripMenuItem.Checked = true;
            小图标ToolStripMenuItem1.Checked = true;
 
        }
        //列表显示按钮事件
        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearCheckStatue();
            listView磁盘文件.View = View.List;
            ListUpdate(FilePathStr);
            列表ToolStripMenuItem.Checked = true;
            列表ToolStripMenuItem1.Checked = true;
 
        }
        //详细信息显示按钮事件
        private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearCheckStatue();
            listView磁盘文件.View = View.Details;
            ListUpdate(FilePathStr);
            详细信息ToolStripMenuItem.Checked = true;
            详细信息ToolStripMenuItem1.Checked = true;
        }
        //平铺信息显示按钮事件
        private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearCheckStatue();
            listView磁盘文件.View = View.Tile;
            ListUpdate(FilePathStr);
            标题ToolStripMenuItem.Checked = true;
            标题ToolStripMenuItem1.Checked = true;
            listView磁盘文件.Refresh();
        }
        //清理选定状态
        private void ClearCheckStatue()
        {
            大图标ToolStripMenuItem.Checked = false;
            小图标ToolStripMenuItem.Checked = false;
            详细信息ToolStripMenuItem.Checked = false;
            列表ToolStripMenuItem.Checked = false;
            标题ToolStripMenuItem.Checked = false;
            大图标ToolStripMenuItem1.Checked = false;
            小图标ToolStripMenuItem1.Checked = false;
            详细信息ToolStripMenuItem1.Checked = false;
            列表ToolStripMenuItem1.Checked = false;
            标题ToolStripMenuItem1.Checked = false;
        }
        #endregion
 
        #region 右键菜单事件
        /// <summary>
        /// 在指定目录新建一个文件
        /// </summary>
        /// <param name="path">文件存放绝对路径</param>
        /// <param name="name">extension</param>
        /// <param name="extension">文件扩展名,类型</param>
        /// <returns></returns>
        public string CreateText(string path, string name, string extension)
        {
            int i = 0;
            String newpath = string.Format("{0}\\{1}{2}",path , name, extension);
            while (File.Exists(newpath))
            {
                i  ;
                newpath = string.Format("{0}\\{1}{2}{3}",path , name , i ,extension);
            }
            File.CreateText(newpath);
            //更新ListView1;
            ListUpdate(TxtAddress.Text);
            //设置可编辑标签模式
            listView磁盘文件.LabelEdit = true;
            //编辑
 
            newpath = newpath.Replace("\\\\", "\\");
            listView磁盘文件.Items[newpath].BeginEdit();
            return newpath;
        }
        /// <summary>
        /// 删除指定文件夹(或者文件)
        /// </summary>
        /// <param name="strFile">文件完整路径</param>
        public void DeleteDir(string strFile)
        {
            try
            {
                //是文件不是文件夹
                if (File.Exists(strFile))
                {
                    File.Delete(strFile);
                }
                    //是文件夹不是文件
                else
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(strFile);
                    //这里直接传参true表示删除其中子目录和文件夹;
                    //dirInfo.Delete(true);
                    //也可以自己递归实现
                    FileInfo[] files = dirInfo.GetFiles();
                    DirectoryInfo[] dirs = dirInfo.GetDirectories();
                    //此目录下有文件夹
 
                    foreach (FileInfo file in files)
                    {
                        DeleteDir(file.FullName);
                    }
                    //删除所有文件夹
                    foreach (DirectoryInfo dir in dirs)
                    {
                        DeleteDir(dir.FullName);
                    }
 
                    Directory.Delete(strFile);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
 
        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult tem = MessageBox.Show(string.Format("   确定删除?"), string.Format("提醒"), MessageBoxButtons.OKCancel);
            if (tem == DialogResult.OK)
            {
                DeleteDir(listView磁盘文件.SelectedItems[0].Name);
            }
            ListUpdate(TxtAddress.Text);
 
        }
 
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView磁盘文件_DoubleClick(sender, e);
        }
 
        //复制文件或者文件夹
        private void CopyFolder(string from, string to)
        {
            to = to.Replace("\\\\", "\\");
            if (!Directory.Exists(to))
                Directory.CreateDirectory(to);
 
            // 子文件夹
            foreach (string sub in Directory.GetDirectories(from))
                CopyFolder(sub   "\\", to   Path.GetFileName(sub)   "\\");
 
            // 文件
            foreach (string file in Directory.GetFiles(from))
                File.Copy(file, to   Path.GetFileName(file), true);
        }
        //记录要复制的文件名
        private string _temName = "";
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                _temName = listView磁盘文件.SelectedItems[0].Text;
                CopyFolder(listView磁盘文件.SelectedItems[0].Name, "C:\\");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CopyFolder("C:\\", TxtAddress.Text   "\\"   _temName   "\\");
                DeleteDir("C:\\");
                ListUpdate(TxtAddress.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("文件读取错误"   ex.Message));
            }
        }
 
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CopyFolder(listView磁盘文件.SelectedItems[0].Name, "C:\\");
                DeleteDir(listView磁盘文件.SelectedItems[0].Name);
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
        }
        //新建文件夹如果存在则文件名 
        private void 文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = 0;
            String newpath = string.Format("{0}\\新建文件夹", TxtAddress.Text);
            while (Directory.Exists(newpath))
            {
                i  ;
                newpath = string.Format("{0}\\新建文件夹{1}",TxtAddress.Text , i);
 
            }
            Directory.CreateDirectory(newpath);
            ListUpdate(TxtAddress.Text);
            listView磁盘文件.LabelEdit = true;
            newpath = newpath.Replace("\\\\", "\\");
            listView磁盘文件.Items[newpath].BeginEdit();
 
        }
 
        private void 文档txtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateText(TxtAddress.Text, "新建文件", ".txt");
 
        }
        private void 文档docToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateText(TxtAddress.Text, "新建文件", ".doc");
        }
        private void 重MING名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView磁盘文件.LabelEdit = true;
            listView磁盘文件.SelectedItems[0].BeginEdit();
        }
        private void 删除队列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int deletecount = listView选中文件.SelectedItems.Count;
            int count = listView选中文件.Items.Count;
            foreach (ListViewItem item in listView选中文件.SelectedItems)
            {
                listView选中文件.Items.Remove(item);
            }
            DoInformation.Text = string.Format("已删除{0}个传输队列文件(剩余{1}个文件)", deletecount, listView选中文件.Items.Count);
        }
         private void 刷新ToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (TxtAddress.Text.Trim().Equals("我的电脑"))
            {
                return;
            }
 
            ListUpdate(TxtAddress.Text.Trim());
        }
        #endregion
 
        #region 其他事件
        //双击打开磁盘文件文件
        private void listView磁盘文件_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if(listView磁盘文件.SelectedItems.Count==0)
                    return;
                string newpath = listView磁盘文件.SelectedItems[0].Name;
                if (newpath.Equals("上一层"))
                {
                    Btn向上Click(null,null);
                }
                else if (Directory.Exists(newpath))
                {
                    ListUpdate(newpath);
                }
                else
                {
                    Process.Start(newpath);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
 
        private void listView磁盘文件_MouseUp(object sender, MouseEventArgs e)
        {
            GetInfomation();
        }
        /// <summary>
        /// 更新选中的信息
        /// </summary>
        public void GetInfomation()
        {
            try
            {
                long filesize = 0;
                int isup = 0;
                DirectoryInfo currentDir = new DirectoryInfo(FilePathStr);
                if (FilePathStr == "我的电脑")
                {
                    for (int i = 0; i < listView磁盘文件.SelectedItems.Count; i  )
                    {
                        if (listView磁盘文件.SelectedItems[i].Tag != null)
                        {
                            filesize  = (long)listView磁盘文件.SelectedItems[i].Tag;
                        }
                    }
                    if (listView磁盘文件.SelectedItems.Count == 0)
                        lbinformation.Text = string.Format("共{0}个对象",
                            listView磁盘文件.Items.Count);
                    else
                  lbinformation.Text = string.Format("共计{0}个对象 (已选中{1}个对象,共{2})", listView磁盘文件.Items.Count, listView磁盘文件.SelectedItems.Count,
                            FileSize(filesize));
                    return;
                }
                DirectoryInfo[] dirs = currentDir.GetDirectories(); //获取目录
                FileInfo[] files = currentDir.GetFiles(); //获取文件
                 
                if (listView磁盘文件.SelectedItems.Count > 0)
                {
                    for (int i = 0; i < listView磁盘文件.SelectedItems.Count; i  )
                    {
                        if (listView磁盘文件.SelectedItems[i].Name.Equals("上一层"))
                        {
                            isup = 1;
                            continue;
                        }
                         
                        if (listView磁盘文件.SelectedItems[i].Tag != null)
                        {
                            filesize  = (long) listView磁盘文件.SelectedItems[i].Tag;
                        }
                    }
                    if ((listView磁盘文件.SelectedItems.Count - isup) == 0)
                        lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length,
                            listView磁盘文件.Items.Count);
                    else
                        lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象 (已选中{3}个对象,共{4})", dirs.Length,
                            files.Length, listView磁盘文件.Items.Count, listView磁盘文件.SelectedItems.Count - isup,
                            FileSize(filesize));
                }
                else
                {
                    lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length,
                        listView磁盘文件.Items.Count);
                }
            }
            catch(Exception ex)
            {
                lbinformation.Text = string.Format(ex.Message);
            }
            DoInformation.Text = listView选中文件.Items.Count == 0 ? "本地浏览器" : string.Format("(传输队列中共{0}个文件)",listView选中文件.Items.Count);
             
        }
 
        private void listView磁盘文件_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetInfomation();
        }
        //右键弹出菜单栏
        private void listView选中文件_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip2.Show(listView选中文件, new Point(e.X, e.Y));
            }
        }
        //Delete键删除队列和全选选中文件
        private void listView选中文件KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                删除队列ToolStripMenuItem_Click(null, null);
            }
            if(e.Control&&e.KeyCode==Keys.A) 
            {
                for (int i = 0; i < listView选中文件.Items.Count; i  )
                {
                    listView选中文件.Items[i].Selected=true;
                }
                 
            }
        }
        //磁盘文件全选
        private void listView磁盘文件KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
            {
                for (int i = 0; i < listView磁盘文件.Items.Count; i  )
                {
                    listView磁盘文件.Items[i].Selected = true;
                }
 
            }
        }
        //激活控件时触发事件
        private void EnterEven(object sender, EventArgs e)
        {
            StatePanel.BackColor = Color.YellowGreen;
            if (listView选中文件.Items.Count > 0)
            {
                Btn传输.Enabled = true;
            }
            else
            {
                Btn传输.Enabled = false;
            }
        }
        //失去焦点时出发事件
        private void LeaveEven(object sender, EventArgs e)
        {
            StatePanel.BackColor = Color.Silver;
            if (listView选中文件.Items.Count > 0)
            {
                Btn传输.Enabled = true;
            }
            else
            {
                Btn传输.Enabled = false;
            }
        }
        #endregion
      
        #region 拖拽文件
        private void listView磁盘文件_ItemDrag(object sender, ItemDragEventArgs e)
        {
            //如果是鼠标右键则返回
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            //判断当前控件里是否有item,如果没有则返回
            if (listView磁盘文件.SelectedIndices.Count <= 0)
            {
                return;
            }
            ListViewItem[] itemTo = new ListViewItem[((ListView)sender).SelectedItems.Count];
            for (int i = 0; i < itemTo.Length; i  )
            {
                itemTo[i] = ((ListView)sender).SelectedItems[i];
            }
            ((ListView)(sender)).DoDragDrop(itemTo, DragDropEffects.Copy);
        }
        //远程文件列表有item拖进时事件
        private void listView选中文件_DragEnter(object sender, DragEventArgs e)
        {
            //判断是否目前拖动的数据是ListViewItem,如果是,则拖动符串对目的组件进行拷贝
            if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]"))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        //远程文件列表的DragDrop事件
        private void listView选中文件_DragDrop(object sender, DragEventArgs e)
        {
            ListViewItem[] items = (ListViewItem[])(e.Data.GetData("System.Windows.Forms.ListViewItem[]"));
            AddFile(items);
        }
        //添加文件
        private void AddFile(ListViewItem[] items)
        {
             
            StringBuilder strBuilder = new StringBuilder();
            int DropCount = 0;
            foreach (ListViewItem item in items)
            {
                ListViewItem temp = new ListViewItem(item.SubItems[0].Text, item.ImageIndex);
                temp.ImageKey = item.ImageKey;
                temp.Tag = item;
                temp.Text = item.SubItems[0].Text;
                temp.SubItems.Add(item.SubItems[0].Name);
                temp.SubItems.Add(SitPath);
                temp.SubItems.Add(FileSize(item.Tag == null ? 0 : (long) item.Tag));
                temp.SubItems.Add(string.Format("上传至服务器:{0}", ServerName));
                if (item.Tag != null && !IsInlistView选中文件(item.SubItems[0].Name)&&FilePathStr!="我的电脑")
                {
                    DropCount  ;
                    temp.ForeColor = Color.DarkSalmon;
                    //temp.ForeColor = Color.Green;
                    strBuilder.Remove(0, strBuilder.Length);
                    strBuilder.Append(Path.GetExtension(temp.SubItems[0].Text));
                    listView选中文件.Items.Add(temp);
                }
            }
            listView选中文件.Refresh();
            DoInformation.Text = (items.Length - DropCount) > 0
                ? string.Format("成功选定{0}个传输文件,{1}个文件已存在传输队列中(传输队列中共{2}个文件)", DropCount, items.Length - DropCount, listView选中文件.Items.Count)
                : string.Format("成功选定{0}个传输文件(传输队列中共{1}个文件)", DropCount, listView选中文件.Items.Count);
            if (listView磁盘文件.SelectedItems.Count>0||FilePathStr == "我的电脑")
            {
                DoInformation.Text = "磁盘不可当做文件拖动到传输队列中";
                return;
            }
        }
 
        /// <summary>
        /// 判断listView选中文件中是否已有该项
        /// </summary>
        /// <returns></returns>
        public bool IsInlistView选中文件(string fullpath)
        {
            bool ishave = false;
            foreach (ListViewItem temp in listView选中文件.Items)
            {
                if (temp.SubItems[1].Text.Equals(fullpath))
                {
                    ishave = true;
                }
            }
            return ishave;
        }
 
        #endregion  
 
       
 
         
 
 
 
        
 
    }
}


标签: C# c 源码 管理

实例下载地址

C#非常强大的资源管理器(源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警