| 做的c#資源管理器所用到的一些事件和方法:一些方法需要自己去掌握。比如怎樣把系統定義的 枚舉轉化成自己想要的枚舉類型。怎樣展開節點,怎樣判斷一個文件是不是隱藏文件等等一些很 小的細節需要自己慢慢去積累去學習。下面是個例子幫助自己學習和理解這些小知識。 //遍歷所有的磁盤 ??????????? foreach (string log in Directory.GetLogicalDrives()) ??????????? { ??????????????? //得到磁盤符的類型 ??????????????? DriveInfo dif = new DriveInfo(log); ??????????????? if (dif.DriveType == DriveType.CDRom)//如果為驅動器的設置樹形節點的 圖標 ??????????????? { ??????????????????? treeView1.Nodes.Add(log, log, 3, 3); ??????????????? } ??????????????? else ??????????????? { ??????????????????? TreeNode tn = treeView1.Nodes.Add(log, log, 2, 2);//設置如果不是 驅動器的樹形節點的圖標 ??????????????????? foreach (string logs in Directory.GetDirectories(log)) ??????????????????? { ??????????????????????? DirectoryInfo difs = new DirectoryInfo(logs); ??????????????????????? if (!difs.Attributes.ToString().Contains("Hidden"))//判斷如 果不是隱藏文件 ??????????????????????? { ??????????????????????????? tn.Nodes.Add(logs, Path.GetFileName(logs), 0, 1); ??????????????????????? } ??????????????????? } ??????????????? } ??????????? } ??????????? foreach (string view in Enum.GetNames(typeof(ViewCN))) ??????????? { ??????????????? toolStripSplitButton3.DropDownItems.Add(view).Click += ViewClick;// 遍歷所有的枚舉類型把他轉化成我們自定義的類型 ??????????? } void ViewClick(object sender, EventArgs e) ??????? { ??????????? listView1.View = (View)(int)((ViewCN)Enum.Parse(typeof(ViewCN), ((ToolStripMenuItem)sender).Text));//把所有的枚舉類型把他轉化成自定義的類型 ??????? } ??????? //自定義與系統給定的一致的枚舉類型 ??????? enum ViewCN ??????? { ??????????? 大圖標 = 0, ??????????? 詳細列表 = 1, ??????????? 小圖標 = 2, ??????????? 列表 = 3, ??????????? 平鋪 = 4, ??????? } ?//在樹形節點被展開后得到它的文件名稱 ??????? private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) ??????? { ??????????? toolStripComboBox1.Text = e.Node.Name; ??????????? foreach (TreeNode tn in e.Node.Nodes) ??????????? { ??????????????? try ??????????????? { ??????????????????? foreach (string str in Directory.GetDirectories(tn.Name)) ??????????????????? { ??????????????????????? DirectoryInfo dif = new DirectoryInfo(str); ??????????????????????? if (!dif.Attributes.ToString().Contains("Hidden")) ??????????????????????? { ??????????????????????????? tn.Nodes.Add(str, Path.GetFileName(str), 0, 1); ??????????????????????????? //tn.Nodes.Add(str, Path.GetFileName(str), 1, 2); ??????????????????????? } ??????????????????? } ??????????????? } ??????????????? catch ??????????????? { ??????????????? } ??????????? } ??????? } ??????? //雙擊listView時讓對應的節點文件也展開 ??????? private void listView1_DoubleClick(object sender, EventArgs e) ??????? { ??????????? if (listView1.SelectedItems.Count > 0) ??????????? { ??????????????? string path = listView1.SelectedItems[0].Name; ??????????????? toolStripComboBox1.Text = path; ??????????????? GetDirFile(path); ??????????????? string[] DirArr = path.Split('\\', '/'); ??????????????? foreach (TreeNode tn in treeView1.Nodes) ??????????????? { ??????????????????? if (DirArr[tn.Level] == tn.Text.Trim('\\'))? //判斷數組的內容和節點的內容是否一致 ??????????????????? { ??????????????????????? tn.Expand();//節點展開 ??????????????????????? ExpadNode(tn, DirArr);//一致遞歸調用展開所有的符合條件的節點 ??????????????????? } ??????????????? } ??????????? } ??????? } ??????? /// <summary> ??????? /// 展開節點方法 ??????? /// </summary> ??????? /// <param name="tn"></param> ??????? /// <param name="DirArr"></param> ??????? void ExpadNode(TreeNode tn, string[] DirArr) ??????? { ??????????? foreach (TreeNode tns in tn.Nodes) ??????????? { ??????????????? if (tns.Level < DirArr.Length) ??????????????? { ??????????????????? if (DirArr[tns.Level] == tns.Text) ??????????????????? { ??????????????????????? tns.Expand(); ??????????????????????? ExpadNode(tns, DirArr); ??????????????????????? break; ??????????????????? } ??????????????? } ??????????? } ??????? } ??????? private void Form1_FormClosing(object sender, FormClosingEventArgs e) ??????? { ??????????? Environment.Exit(0);//應用程序退出 ??????? } ??????? //對文件進行重命名 ??????? private void button1_Click(object sender, EventArgs e) ??????? { ??????????? listView1.LabelEdit = true; ??????????? listView1.SelectedItems[0].BeginEdit(); ??????????? //foreach (string str in Directory.GetDirectories(@"C:/")) ??????????? //{ ??????????? //??? DirectoryInfo di = new DirectoryInfo(str); ??????????? //??? MessageBox.Show(str + "---" + di.Attributes.ToString()); ??????????? //} ??????? } ??????? //拖文件實現復制的功能 ??????? private void listView1_DragEnter(object sender, DragEventArgs e) ??????? { ??????????? if (e.Data.GetDataPresent(DataFormats.FileDrop)) ??????????? { ??????????????? string ath = (((string[])e.Data.GetData(DataFormats.FileDrop))[0]); ??????????????? File.Copy(ath, toolStripComboBox1.Text + "\\" + Path.GetFileName (ath), true); ??????????? } ??????? } ?//在樹形節點被選擇以后得到它的圖標和名稱 ??????? private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) ??????? { ??????????? GetDirFile(e.Node.Name); ??????????? toolStripComboBox1.Text = e.Node.Name; ??????? } ??????? /// <summary> ??????? /// 大小圖片的轉化和引用系統的文件轉化圖標 ??????? /// </summary> ??????? /// <param name="path"></param> ??????? void GetDirFile(string path) ??????? { ??????????? listView1.Items.Clear(); ??????????? SmallImaList.Images.Clear(); ??????????? LargeImaList.Images.Clear(); ??????????? int nIndex = 0; ??????????? SHFILEINFO shinfo = new SHFILEINFO(); ??????????? listView1.SmallImageList = SmallImaList; ??????????? listView1.LargeImageList = LargeImaList; ??????????? try ??????????? { ??????????????? foreach (string file in Directory.GetFiles(path)) ??????????????? { ??????????????????? Win32.SHGetFileInfo(file, 0, ref shinfo, (uint)Marshal.SizeOf (shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); ??????????????????? Icon myIcon = Icon.FromHandle(shinfo.hIcon); ??????????????????? this.SmallImaList.Images.Add(myIcon); ??????????????????? Win32.SHGetFileInfo(file, 0, ref shinfo, (uint)Marshal.SizeOf (shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); ??????????????????? myIcon = Icon.FromHandle(shinfo.hIcon); ??????????????????? this.LargeImaList.Images.Add(myIcon); ??????????????????? FileInfo fi = new FileInfo(file); ??????????????????? if (!fi.Attributes.ToString().Contains("Hidden")) ??????????????????? { ??????????????????????? ListViewItem lvi = listView1.Items.Add(file, Path.GetFileName(file), nIndex++); ??????????????????????? lvi.SubItems.Add(Directory.GetLastWriteTime(file).ToString ()); ??????????????????????? lvi.SubItems.Add(Path.GetExtension(file)); ??????????????????? } ??????????????? } ??????????? } ??????????? catch ??????????? { } ??????????? SmallImaList.Images.Add(imageList1.Images[0]); ??????????? LargeImaList.Images.Add(imageList1.Images[0]); ??????????? try ??????????? { ??????????????? foreach (string dir in Directory.GetDirectories(path)) ??????????????? { ??????????????????? DirectoryInfo dif = new DirectoryInfo(dir); ??????????????????? if (!dif.Attributes.ToString().Contains("Hidden")) ??????????????????? { ??????????????????????? ListViewItem lvi = listView1.Items.Add(dir, Path.GetFileName(dir), SmallImaList.Images.Count - 1); ??????????????????????? lvi.SubItems.Add(Directory.GetLastWriteTime(dir).ToString ()); ??????????????????????? lvi.SubItems.Add("文件夾"); ??????????????????? } ??????????????? } ??????????? } ??????????? catch ??????????? { } ??????? } ///引用系統文件獲得系統文件對應的圖標 ??? [StructLayout(LayoutKind.Sequential)] ??? public struct SHFILEINFO ??? { ??????? public IntPtr hIcon; ??????? public IntPtr iIcon; ??????? public uint dwAttributes; ??????? [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] ??????? public string szDisplayName; ??????? [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] ??????? public string szTypeName; ??? } ??? class Win32 ??? { ??????? public const uint SHGFI_ICON = 0x100; ??????? public const uint SHGFI_LARGEICON = 0x0; // 'Large icon ??????? public const uint SHGFI_SMALLICON = 0x1; // 'Small icon ??????? [DllImport("shell32.dll")] ??????? public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); ??? } 本文出自 “zhangjingjing” 博客,請務必保留此出處http://zhjjzhjj.blog.51cto.com/1802676/379525 |