版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhjjzhjj.blog.51cto.com/1802676/379525

做的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

posted on 2010-08-21 17:04  程序诗人  阅读(566)  评论(0编辑  收藏  举报