资源简介
使用双缓冲TreeView,listView,实现目录文件浏览,代码简单400行,不闪烁,可显示文件夹不同状态图标,可应用于自定义的文件对话框等,可根据需要自行继续简化。
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Management; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Disk1 { public partial class Form1 : Form { public static class getIcon { [DllImport("Shell32.dll")] static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags); struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public int dwAttributes; public char szDisplayName; public char szTypeName; } static public Icon GetFileIcon(string fileName, bool smallIcon) { SHFILEINFO fi = new SHFILEINFO(); Icon ic = null; int iTotal = (int)SHGetFileInfo(fileName, 100, ref fi, 0, (uint)(smallIcon ? 273 : 272)); if (iTotal > 0) { ic = Icon.FromHandle(fi.hIcon); } return ic; } } public class TreeViewEx : TreeView { protected override void OnHandleCreated(EventArgs e) { SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER); base.OnHandleCreated(e); } private const int TVM_SETEXTENDEDSTYLE = 0x1100 44; private const int TVM_GETEXTENDEDSTYLE = 0x1100 45; private const int TVS_EX_DOUBLEBUFFER = 0x0004; [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); } public class ListViewEx : ListView { public ListViewEx() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); } } TreeViewEx treeView1; ListViewEx listView1; public static int BrowNumber = 0, BrowRecord = 0, AssNumber = 0; public class RecordBrow { public string Dir_Name; public RecordBrow(string _name) { Dir_Name = _name; BrowNumber = 1; } } public static List<RecordBrow> recordBrows = new List<RecordBrow>(); public class FileAssociated { public int Id; public string AssociatedType; public Bitmap AssociatedIcon; public FileAssociated(int _id, string _type, Bitmap _icon) { Id = _id; AssociatedType = _type; AssociatedIcon = _icon; } } public static List<FileAssociated> fileAssociateds = new List<FileAssociated>(); public class DirectoryFile { public string Fullname; public string DFname; public string DFtype; public int DFicon; public string DFtime; public long DFsize; public int DFflag; public DirectoryFile(string _full, string _name, string _type,int _icon, string _time,long _size,int _flag) { Fullname = _full; DFname = _name; DFtype = _type; DFicon = _icon; DFtime = _time; DFsize = _size; DFflag = _flag; } } public static List<DirectoryFile> directoryFiles = new List<DirectoryFile>(); public Form1() { InitializeComponent(); treeView1 = new TreeViewEx() { ItemHeight = 22, Size = new Size(376, 546), Location = new Point(12, 41), Font = new Font("微软雅黑", 9), ImageList = imageList1, ShowLines = false, ShowRootLines = false, ShowPlusMinus = false, }; treeView1.NodeMouseClick = treeView1_NodeMouseClick; this.Controls.Add(treeView1); listView1 = new ListViewEx() { Size = new Size(515, 547), Location = new Point(442, 40), Font = new Font("微软雅黑", 9), View = View.Details, GridLines = false, SmallImageList = imageList2, FullRowSelect = true, }; listView1.Columns.Add("文件名", 120, HorizontalAlignment.Left); listView1.Columns.Add("文件类型", 120, HorizontalAlignment.Left); listView1.Columns.Add("修改时间", 120, HorizontalAlignment.Left); listView1.Columns.Add("大小", 120, HorizontalAlignment.Left); listView1.DoubleClick = ListView1_DoubleClick; this.Controls.Add(listView1); ManagementObjectCollection queryCollection = getDrives(); treeView1.Nodes.Clear(); foreach (ManagementObject mo in queryCollection) { TreeNode node1 = new TreeNode(); node1.Text = mo["Name"].ToString(); node1.Name = mo["Name"].ToString() "\\"; node1.ImageIndex = 0; node1.Tag = 0; treeView1.Nodes.Add(node1); } get_SubDirFile(treeView1.Nodes[0].Name); SearchNode(treeView1.Nodes, treeView1.Nodes[0].Name); recordBrows.Add(new RecordBrow(treeView1.Nodes[0].Name)); } protected ManagementObjectCollection getDrives() { ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk "); ManagementObjectCollection queryCollection = query.Get(); return queryCollection; } public void get_SubDirFile(string _path) { directoryFiles.Clear(); listView1.Items.Clear(); DirectoryInfo dir = new DirectoryInfo(_path); try { DirectoryInfo[] subDirector = dir.GetDirectories(); for (int i = 0; i < subDirector.Length; i ) { directoryFiles.Add(new DirectoryFile(subDirector[i].FullName, subDirector[i].FullName.Replace(_path, "").Replace("\\", "") ,"文件夹",0 , subDirector[i].LastWriteTime.ToShortDateString() subDirector[i].LastWriteTime.ToShortTimeString(),0,0)); } } catch { } DirectoryInfo Files = new DirectoryInfo(_path); try { FileInfo[] files = Files.GetFiles("*.*"); for (int i = 0; i < files.Length; i ) { string _type = GetFileTypeDesection(files[i].Extension); int img_Index = 0; var _fa = fileAssociateds.Where(fa=>fa.AssociatedType==_type).ToList(); if (_fa.Count<1) { Bitmap _bmp = new Bitmap(20, 20); Graphics g = Graphics.FromImage(_bmp); g.DrawImage(getIcon.GetFileIcon(files[i].FullName, true).ToBitmap(),new PointF(2,2)); imageList2.Images.Add(_bmp); AssNumber = 1; img_Index = AssNumber; } else { img_Index = _fa[0].Id 1; } directoryFiles.Add(new DirectoryFile(files[i].FullName, files[i].FullName.Replace(_path, "").Replace("\\", ""), _type, img_Index, files[i].LastWriteTime.ToShortDateString() files[i].LastWriteTime.ToShortTimeString(), files[i].Length,1)); } } catch { } listView1.BeginUpdate(); for (int i=0;i<directoryFiles.Count;i ) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = directoryFiles[i].DFicon; lvi.Text = directoryFiles[i].DFname; lvi.SubItems.Add(directoryFiles[i].DFtype); lvi.SubItems.Add(directoryFiles[i].DFtime); if (directoryFiles[i].DFflag==0) lvi.SubItems.Add(""); else lvi.SubItems.Add(directoryFiles[i].DFsize.ToString("N0")); listView1.Items.Add(lvi); } listView1.EndUpdate(); } public void get_SubDir() { if (treeView1.SelectedNode.Nodes.Count < 1) { DirectoryInfo dir = new DirectoryInfo(treeView1.SelectedNode.Name); try { DirectoryInfo[] subDirector = dir.GetDirectories(); for (int i = 0; i < subDirector.Length; i ) { TreeNode node1 = new TreeNode(); node1.Name = subDirector[i].FullName; node1.Tag = 1; node1.ImageIndex = 2; node1.SelectedImageIndex = 2; DirectoryInfo has_sub = new DirectoryInfo(node1.Name); try { DirectoryInfo[] hasDir = has_sub.GetDirectories(); if (hasDir.Length < 1) { node1.ImageIndex = 4; node1.SelectedImageIndex = 4; node1.Tag = 4; } } catch { node1.ImageIndex = 4; node1.SelectedImageIndex = 4; node1.Tag = 4; } node1.Text = node1.Name.Replace(treeView1.SelectedNode.Name, "").Replace("\\", ""); treeView1.SelectedNode.Nodes.Add(node1); } } catch { } } } private void SearchNode(TreeNodeCollection tnds, string text) { foreach (TreeNode tnd in tnds) { if (tnd.Name.Equals(text)) { treeView1.SelectedNode = tnd; break; } if (tnd.Nodes.Count != 0) { SearchNode(tnd.Nodes, text); } } textBox1.Text = text; treeView1.Focus(); } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { Cursor = Cursors.WaitCursor; get_SubDirFile(e.Node.Name); SearchNode(treeView1.Nodes, e.Node.Name); get_SubDir(); if (e.Node.Nodes.Count < 1) { if ((int)e.Node.Tag == 0) { e.Node.SelectedImageIndex = 0; e.Node.ImageIndex = 0; } else if ((int)e.Node.Tag == 1) { e.Node.SelectedImageIndex = 4; e.Node.ImageIndex = 4; } } else { if (e.Node.IsExpanded) { e.Node.Collapse(); if ((int)e.Node.Tag == 0) { e.Node.SelectedImageIndex = 0; e.Node.ImageIndex = 0; } else if ((int)e.Node.Tag == 1) { e.Node.SelectedImageIndex = 2; e.Node.ImageIndex = 2; } setchild(e.Node); } else { e.Node.Expand(); if ((int)e.Node.Tag == 0) { e.Node.SelectedImageIndex = 1; e.Node.ImageIndex = 1; } else if ((int)e.Node.Tag == 1) { e.Node.SelectedImageIndex = 3; e.Node.ImageIndex = 3; } } } Cursor = Cursors.Default; if (e.Node.Name!=recordBrows[BrowNumber-1].Dir_Name) // 最后浏览记录不相同就记录 { BrowRecord = 1; recordBrows.Add( new RecordBrow(e.Node.Name) ); } } private void ListView1_DoubleClick(object sender, EventArgs e) { var items = listView1.SelectedItems; Cursor = Cursors.WaitCursor; int _index = listView1.Items[listView1.SelectedIndices[0]].Index; if (directoryFiles[_index].DFflag == 0) { if (directoryFiles[_index].Fullname != recordBrows[BrowNumber - 1].Dir_Name) { BrowRecord = 1; recordBrows.Add(new RecordBrow(directoryFiles[_index].Fullname)); } textBox1.Text = directoryFiles[_index].Fullname; SearchNode(treeView1.Nodes, directoryFiles[_index].Fullname); get_SubDirFile(directoryFiles[_index].Fullname); get_SubDir(); } Cursor = Cursors.Default; } private void setchild(TreeNode node) { foreach (TreeNode child in node.Nodes) { if ((int)child.Tag == 0) { child.ImageIndex = 0; } else if ((int)child.Tag == 1) { child.ImageIndex = 2; } setchild(child); } } // 前进 private void button1_Click(object sender, EventArgs e) { int _reco = (BrowRecord 1) > (BrowNumber - 1) ? (BrowNumber - 1) : (BrowRecord 1); if (_reco != BrowRecord) { BrowRecord = _reco; SearchNode(treeView1.Nodes, recordBrows[BrowRecord].Dir_Name); get_SubDirFile(recordBrows[BrowRecord].Dir_Name); } treeView1.Focus(); } // 后退 private void button2_Click(object sender, EventArgs e) { int _reco = (BrowRecord - 1) < 0 ? 0 : (BrowRecord - 1); if (_reco != BrowRecord) { BrowRecord = _reco; SearchNode(treeView1.Nodes, recordBrows[BrowRecord].Dir_Name); get_SubDirFile(recordBrows[BrowRecord].Dir_Name); } treeView1.Focus(); } public static string GetFileTypeDesection(string _ext) { string value; try { string desc = (string)Registry.ClassesRoot.OpenSubKey(_ext).GetValue(null); if (desc != null) value = (string)Registry.ClassesRoot.OpenSubKey(desc).GetValue(null); else value = _ext.Replace(".", "") " 文件"; } catch { value = _ext.Replace(".","") " 文件"; } return value; } } }
代码片段和文件信息
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Disk1
{
public partial class Form1 : Form
{
public static class getIcon
{
[DllImport(“Shell32.dll“)]
static extern int SHGetFileInfo(string pszPath uint dwFileAttributes ref SHFILEINFO psfi int cbFileInfo uint uFlags);
struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
public char szDisplayName;
public char szTypeName;
}
sta
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 53760 2019-02-03 19:23 Disk1\.vs\Disk1\v15\.suo
文件 0 2019-01-30 08:00 Disk1\.vs\Disk1\v15\Server\sqlite3\db.lock
文件 626688 2019-02-03 11:35 Disk1\.vs\Disk1\v15\Server\sqlite3\storage.ide
文件 32768 2019-02-03 18:55 Disk1\.vs\Disk1\v15\Server\sqlite3\storage.ide-shm
文件 4140632 2019-02-03 19:23 Disk1\.vs\Disk1\v15\Server\sqlite3\storage.ide-wal
文件 189 2019-01-30 08:00 Disk1\Disk1\App.config
文件 27136 2019-02-03 19:23 Disk1\Disk1\bin\Debug\Disk1.exe
文件 189 2019-01-30 08:00 Disk1\Disk1\bin\Debug\Disk1.exe.config
文件 42496 2019-02-03 19:23 Disk1\Disk1\bin\Debug\Disk1.pdb
文件 3645 2019-02-03 19:20 Disk1\Disk1\Disk1.csproj
文件 16223 2019-02-03 19:23 Disk1\Disk1\Form1.cs
文件 5522 2019-02-03 19:20 Disk1\Disk1\Form1.Designer.cs
文件 19639 2019-02-03 19:20 Disk1\Disk1\Form1.resx
文件 1443 2019-02-03 07:54 Disk1\Disk1\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7168 2019-02-03 19:20 Disk1\Disk1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 42 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.csproj.CoreCompileInputs.cache
文件 978 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.csproj.FileListAbsolute.txt
文件 1012 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.csproj.GenerateResource.cache
文件 27136 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.exe
文件 9054 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.Form1.resources
文件 42496 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.pdb
文件 180 2019-02-03 19:23 Disk1\Disk1\obj\Debug\Disk1.Properties.Resources.resources
文件 0 2019-02-02 08:00 Disk1\Disk1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2019-02-02 08:00 Disk1\Disk1\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2019-02-02 08:00 Disk1\Disk1\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 517 2019-01-30 08:00 Disk1\Disk1\Program.cs
文件 1308 2019-01-30 08:00 Disk1\Disk1\Properties\AssemblyInfo.cs
文件 2823 2019-01-30 08:00 Disk1\Disk1\Properties\Resources.Designer.cs
文件 5612 2019-01-30 08:00 Disk1\Disk1\Properties\Resources.resx
文件 1092 2019-01-30 08:00 Disk1\Disk1\Properties\Settings.Designer.cs
............此处省略20个文件信息
相关资源
- gmap离线地图执行文件
- jena_climate_2009_2016耶拿天气数据集
- 基于ZigBee的智能家居系统项目
- Asp.net C#生成pdf文件
- C# 将office,PDF文件转换为SWF文件
- C#学生选课系统包括源程序及数据库文
- WinForm窗体程序中使用CefSharp获取加载
- TeeChart for .NET 2017 Evaluation 4.1.2017.031
-
C#将数据库数据生成xm
l文件 - 大文件上传 带进度条 asp.net
- VS中NuGet包离线安装CefSharp.WinForms.49.
- Q692791问题的回答,https://ask.csdn.net/
- C#.kgtemp转换.mp3文件源码请勿商用
- python 天气CSV文件
- WebZip乱码目录文件名修改
- 原创仿OpenFileDialog文件选择对话框源码
- C# 图像处理、神经网络、遗传算法集
- asp.net 文件上传demo
- C# winform TreeView树形菜单控件DEMO附配套
- 学生信息管理系统VS2010版文件
- C#俄罗斯方块源文件和资源文件
- WPF MVVM模式下 TreeView 右键菜单
- 螺旋天线的CST仿真
- semantic ui 最全的css icon文件
- C# SOCKET加密文件消息传输通信
- c# vtk 读取dicom文件!
- C#读取excel、csv文件到折线图
- 处理后的禁飞区 csv文件
- EEMD程序DLL
- C#局域网内文件传输
评论
共有 条评论