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

C# 仿资源管理器源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.06M
  • 下载次数:43
  • 浏览次数:420
  • 发布时间:2015-08-13
  • 实例类别:C#语言基础
  • 发 布 人:Hellowork
  • 文件格式:.zip
  • 所需积分:10
 相关标签: 管理

实例介绍

【实例简介】

【实例截图】

【核心代码】

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
 
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.ImageList TreeImageList;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ImageList LisrimageList;
        private System.Windows.Forms.ImageList LisrimageList2;
        private System.Windows.Forms.StatusBar statusBar1;
        private System.Windows.Forms.StatusBarPanel statusBarPanel1;
        private System.Windows.Forms.StatusBarPanel statusBarPanel2;
        private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.MenuItem menuItem4;
        private System.Windows.Forms.MenuItem menuItem5;
        private System.Windows.Forms.MenuItem menuItem6;
        private System.Windows.Forms.MenuItem menuItem7;
        private System.Windows.Forms.Label label1;
        //private System.ComponentModel.IContainer components;
 
 
 
 
        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
 
            Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 15);
            TreeImageList.Images.Add(ic0);
            Icon ic1 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 5);
            TreeImageList.Images.Add(ic1);
            Icon ic2 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 7);
            TreeImageList.Images.Add(ic2);
            Icon ic3 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 11);
            TreeImageList.Images.Add(ic3);
 
            Icon ic4 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);
            TreeImageList.Images.Add(ic4);
            Icon ic5 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 4);
            TreeImageList.Images.Add(ic5);
            Icon ic6 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 101);
            TreeImageList.Images.Add(ic6);
 
 
            GetDrive();
        }
 
 
        //*************************************************************************************
        [DllImport("Shell32.dll")]
        public static extern int ExtractIcon(IntPtr h, string strx, int ii);
 
        [DllImport("Shell32.dll")]
        public static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
 
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;
            public char szDisplayName;
            public char szTypeName;
        }
 
 
        string strFilePath = "";
        //*************************************************************************************
 
        protected virtual Icon myExtractIcon(string FileName, int iIndex)
        {
            try
            {
                IntPtr hIcon = (IntPtr)ExtractIcon(this.Handle, FileName, iIndex);
                if (!hIcon.Equals(null))
                {
                    Icon icon = Icon.FromHandle(hIcon);
                    return icon;
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message, "错误提示", 0, MessageBoxIcon.Error); }
            return null;
        }
        //*************************************************************************************
 
        protected virtual void SetIcon(ImageList imageList, string FileName, bool tf)
        {
            SHFILEINFO fi = new SHFILEINFO();
            if (tf == true)
            {
                int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 16640);//SHGFI_ICON|SHGFI_SMALLICON
                try
                {
                    if (iTotal > 0)
                    {
                        Icon ic = Icon.FromHandle(fi.hIcon);
                        imageList.Images.Add(ic);
                        //return ic;
                    }
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message, "错误提示", 0, MessageBoxIcon.Error); }
            }
            else
            {
                int iTotal = (int)SHGetFileInfo(FileName, 0, ref fi, 100, 257);
                try
                {
                    if (iTotal > 0)
                    {
                        Icon ic = Icon.FromHandle(fi.hIcon);
                        imageList.Images.Add(ic);
                        //return ic;
                    }
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message, "错误提示", 0, MessageBoxIcon.Error); }
            }
            // return null;
        }
        //*************************************************************************************
 
        public void GetDrive()
        {
            treeView1.ImageList = TreeImageList;
 
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();
 
            TreeNode RootNode = new TreeNode("我的电脑", 0, 0);
            treeView1.Nodes.Add(RootNode);
 
            int iImageIndex = 2; int iSelectedIndex = 2;
            string[] astrDrives = Directory.GetLogicalDrives();
 
            foreach (string str in astrDrives)
            {
                if (str == "A:\\")
                { iImageIndex = 1; iSelectedIndex = 1; }
                else if (str == "G:\\")
                { iImageIndex = 3; iSelectedIndex = 3; }
                else
                { iImageIndex = 2; iSelectedIndex = 2; }
 
                TreeNode tnDrive = new TreeNode(str, iImageIndex, iSelectedIndex);
                treeView1.Nodes[0].Nodes.Add(tnDrive);
                AddDirectories(tnDrive);
 
                if (str == "C:\\")
                { treeView1.SelectedNode = tnDrive; }
            }
            treeView1.EndUpdate();
        }
        //*************************************************************************************
 
        void AddDirectories(TreeNode tn)
        {
            tn.Nodes.Clear();
 
            string strPath = tn.FullPath;
            strPath = strPath.Remove(0, 5);
 
            //获得当前目录
            DirectoryInfo dirinfo = new DirectoryInfo(strPath);
            DirectoryInfo[] adirinfo;
            try
            {
                adirinfo = dirinfo.GetDirectories();
            }
            catch
            { return; }
 
            int iImageIndex = 4; int iSelectedIndex = 5;
            foreach (DirectoryInfo di in adirinfo)
            {
                if (di.Name == "RECYCLER" || di.Name == "RECYCLED" || di.Name == "Recycled")
                { iImageIndex = 6; iSelectedIndex = 6; }
                else
                { iImageIndex = 4; iSelectedIndex = 5; }
 
                TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
                tn.Nodes.Add(tnDir);
            }
 
 
            /**/
            /*
           //获得当前目录下的所有文件
           FileInfo[] dirFiles;
           dirFiles=dirinfo.GetFiles();
           int iCount=7;
 
           foreach (FileInfo fi in dirFiles)
           {
          //得到每个文件的图标
          string str=fi.FullName;
          try
          {
            SetIcon(TreeImageList,str,false);
          }
          catch(Exception ex)
          { MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
         
          TreeNode tnDir = new TreeNode(fi.Name, iCount, iCount);
          tn.Nodes.Add(tnDir);
 
          iCount  ;
           }
        */
        }
        //*************************************************************************************
        //在大图标、小图标、详细列表切换时调用
        protected virtual void ListViewAB(int iii)
        {
            if (iii == 1)
            {
                LisrimageList2.ImageSize = new Size(32, 32);
                listView1.LargeImageList = LisrimageList2;
 
                if (listView1.View == View.Details || listView1.View == View.SmallIcon)
                { listView1.View = View.LargeIcon; }
            }
            else if (iii == 2)
            {
                if (listView1.View == View.Details || listView1.View == View.LargeIcon)
                { listView1.View = View.SmallIcon; }
            }
            else
            {
                if (listView1.View == View.LargeIcon || listView1.View == View.SmallIcon)
                { listView1.View = View.Details; }
            }
        }
        //*************************************************************************************
        private void treeView1_BeforeExpand_1(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        {
            //base.OnBeforeExpand(e);
 
            treeView1.BeginUpdate();
 
            foreach (TreeNode tn in e.Node.Nodes)
            { AddDirectories(tn); }
 
            treeView1.EndUpdate();
        }
 
        //*************************************************************************************
 
        protected virtual void InitList(TreeNode tn)
        {
            this.statusBarPanel1.Text = "正在刷新文件夹,请稍等..";
            this.Cursor = Cursors.WaitCursor;
 
            this.LisrimageList2.Images.Clear();
            this.LisrimageList.Images.Clear();
            listView1.SmallImageList = LisrimageList;
            Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);
            LisrimageList.Images.Add(ic0);
            LisrimageList2.Images.Add(ic0);
 
            listView1.Clear();
            //设置列表框的表头
            listView1.Columns.Add("文件名", 160, HorizontalAlignment.Left);
            listView1.Columns.Add("文件大小", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("创建时间", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("访问时间", 200, HorizontalAlignment.Left);
 
 
            string strPath = tn.FullPath;
            strPath = strPath.Remove(0, 5);
            //获得当前目录下的所有文件
            DirectoryInfo curDir = new DirectoryInfo(strPath);//创建目录对象。
            FileInfo[] dirFiles;
            try
            {
                dirFiles = curDir.GetFiles();
            }
            catch { return; }
 
            string[] arrSubItem = new string[4];
            //文件的创建时间和访问时间。
            int iCount = 0; int iconIndex = 1;//用1,而不用0是要让过0号图标。
            foreach (FileInfo fileInfo in dirFiles)
            {
                string strFileName = fileInfo.Name;
 
                //如果不是文件pagefile.sys
                if (!strFileName.Equals("pagefile.sys"))
                {
                    arrSubItem[0] = strFileName;
                    arrSubItem[1] = fileInfo.Length   " 字节";
                    arrSubItem[2] = fileInfo.CreationTime.ToString();
                    arrSubItem[3] = fileInfo.LastAccessTime.ToString();
                }
                else
                { arrSubItem[1] = "未知大小"; arrSubItem[2] = "未知日期"; arrSubItem[3] = "未知日期"; }
 
 
                //得到每个文件的图标
                string str = fileInfo.FullName;
                try
                {
                    SetIcon(LisrimageList, str, false);
                    SetIcon(LisrimageList2, str, true);
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message, "错误提示", 0, MessageBoxIcon.Error); }
 
 
                //插入列表项   
                ListViewItem LiItem = new ListViewItem(arrSubItem, iconIndex);
                listView1.Items.Insert(iCount, LiItem);
 
                iCount  ;
                iconIndex  ;
            }
            strFilePath = strPath;
            textBox1.Text = strPath;
            this.statusBarPanel1.Text = strPath;
            this.statusBarPanel2.Text = "文件数量: "   iCount.ToString()   "个";
            this.Cursor = Cursors.Arrow;
 
 
 
            //以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
            int iItem = 0;
 
            DirectoryInfo Dir = new DirectoryInfo(strPath);
            foreach (DirectoryInfo di in Dir.GetDirectories())
            {
                ListViewItem LiItem = new ListViewItem(di.Name, 0);
                listView1.Items.Insert(iItem, LiItem);
                iItem  ;
            }
 
        }
        //*************************************************************************************
 
        protected virtual void InitList2(string strName)
        {
            this.statusBarPanel1.Text = "正在刷新文件夹,请稍等..";
            this.Cursor = Cursors.WaitCursor;
 
            this.LisrimageList2.Images.Clear();
            this.LisrimageList.Images.Clear();
            listView1.SmallImageList = LisrimageList;
            Icon ic0 = myExtractIcon("%SystemRoot%\\system32\\shell32.dll", 3);
            LisrimageList.Images.Add(ic0);
            LisrimageList2.Images.Add(ic0);
 
            listView1.Clear();
            //设置列表框的表头
            listView1.Columns.Add("文件名", 160, HorizontalAlignment.Left);
            listView1.Columns.Add("文件大小", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("创建时间", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("访问时间", 200, HorizontalAlignment.Left);
 
 
            //获得当前目录下的所有文件
            DirectoryInfo curDir = new DirectoryInfo(strName);//创建目录对象。
            FileInfo[] dirFiles;
            try
            {
                dirFiles = curDir.GetFiles();
            }
            catch { return; }
 
            string[] arrSubItem = new string[4];
            //文件的创建时间和访问时间。
            int iCount = 0; int iconIndex = 1;//用1,而不用0是要让过0号图标。
            foreach (FileInfo fileInfo in dirFiles)
            {
                string strFileName = fileInfo.Name;
 
                //如果不是文件pagefile.sys
                if (!strFileName.Equals("pagefile.sys"))
                {
                    arrSubItem[0] = strFileName;
                    arrSubItem[1] = fileInfo.Length   " 字节";
                    arrSubItem[2] = fileInfo.CreationTime.ToString();
                    arrSubItem[3] = fileInfo.LastAccessTime.ToString();
                }
                else
                { arrSubItem[1] = "未知大小"; arrSubItem[2] = "未知日期"; arrSubItem[3] = "未知日期"; }
 
 
                //得到每个文件的图标
                string str = fileInfo.FullName;
                try
                {
                    SetIcon(LisrimageList, str, false);
                    SetIcon(LisrimageList2, str, true);
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message, "错误提示", 0, MessageBoxIcon.Error); }
 
 
                //插入列表项   
                ListViewItem LiItem = new ListViewItem(arrSubItem, iconIndex);
                listView1.Items.Insert(iCount, LiItem);
 
                iCount  ;
                iconIndex  ;//必须加在listView1.Items.Insert(iCount,LiItem);
            }
            strFilePath = strName;//把路径赋值于全局变量strFilePath
 
            textBox1.Text = strName;
            this.statusBarPanel2.Text = "文件数量: "   iCount.ToString()   "个";
            this.Cursor = Cursors.Arrow;
 
 
 
            //以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
            int iItem = 0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
 
            DirectoryInfo Dir = new DirectoryInfo(strName);//创建目录对象。
            foreach (DirectoryInfo di in Dir.GetDirectories())
            {
                ListViewItem LiItem = new ListViewItem(di.Name, 0);
                listView1.Items.Insert(iItem, LiItem);
                iItem  ;
            }
 
        }
        //*************************************************************************************
 
        private void treeView1_AfterSelect_1(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            if (e.Node.Text == "我的电脑")
            { return; }
 
            InitList(e.Node);
        }
        //*************************************************************************************
 
        private void button1_Click(object sender, System.EventArgs e)
        {
            this.panel1.Hide();
        }
        //*************************************************************************************
 
        private void listView1_ItemActivate(object sender, System.EventArgs e)
        {
            string str = Path.Combine(strFilePath, listView1.FocusedItem.Text);
            try
            {
                if (listView1.FocusedItem.SubItems.Count > 1)
                { System.Diagnostics.Process.Start(str); }
                else
                { InitList2(str); }
            }
            catch { return; }
        }
        //*************************************************************************************
        private void menuItem3_Click(object sender, System.EventArgs e)
        {
            this.panel1.Visible = !panel1.Visible;
        }
        //*************************************************************************************
        private void menuItem5_Click(object sender, System.EventArgs e)
        {
            ListViewAB(1);
        }
        //*************************************************************************************
        private void menuItem6_Click(object sender, System.EventArgs e)
        {
            ListViewAB(2);
        }
        //*************************************************************************************
        private void menuItem7_Click(object sender, System.EventArgs e)
        {
            ListViewAB(3);
        }
        //*************************************************************************************
 
 
    }
}

标签: 管理

实例下载地址

C# 仿资源管理器源码

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

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

网友评论

发表评论

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

查看所有0条评论>>

小贴士

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

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

关于好例子网

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

;
报警