一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

c#做的資源管理器 - zhangjingjing - 51CTO技術(shù)博客

 sirianyan 2010-08-21

做的c#資源管理器所用到的一些事件和方法:一些方法需要自己去掌握。比如怎樣把系統(tǒng)定義的

枚舉轉(zhuǎn)化成自己想要的枚舉類型。怎樣展開節(jié)點,怎樣判斷一個文件是不是隱藏文件等等一些很

小的細節(jié)需要自己慢慢去積累去學習。下面是個例子幫助自己學習和理解這些小知識。
//遍歷所有的磁盤
            foreach (string log in Directory.GetLogicalDrives())
            {
                //得到磁盤符的類型
                DriveInfo dif = new DriveInfo(log);

                if (dif.DriveType == DriveType.CDRom)//如果為驅(qū)動器的設置樹形節(jié)點的

圖標
                {
                    treeView1.Nodes.Add(log, log, 3, 3);
                }
                else
                {
                    TreeNode tn = treeView1.Nodes.Add(log, log, 2, 2);//設置如果不是

驅(qū)動器的樹形節(jié)點的圖標
                    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;//

遍歷所有的枚舉類型把他轉(zhuǎn)化成我們自定義的類型
            }

void ViewClick(object sender, EventArgs e)
        {
            listView1.View = (View)(int)((ViewCN)Enum.Parse(typeof(ViewCN),

((ToolStripMenuItem)sender).Text));//把所有的枚舉類型把他轉(zhuǎn)化成自定義的類型
        }
        //自定義與系統(tǒng)給定的一致的枚舉類型
        enum ViewCN
        {
            大圖標 = 0,
            詳細列表 = 1,
            小圖標 = 2,
            列表 = 3,
            平鋪 = 4,
        }


 //在樹形節(jié)點被展開后得到它的文件名稱
        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時讓對應的節(jié)點文件也展開
        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('\\'))  //判斷數(shù)組的內(nèi)容和節(jié)點的內(nèi)容是否一致
                    {
                        tn.Expand();//節(jié)點展開
                        ExpadNode(tn, DirArr);//一致遞歸調(diào)用展開所有的符合條件的節(jié)點
                    }
                }
            }
        }
        /// <summary>
        /// 展開節(jié)點方法
        /// </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());
            //}
        }
        //拖文件實現(xiàn)復制的功能
        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);
            }
        }
 //在樹形節(jié)點被選擇以后得到它的圖標和名稱
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            GetDirFile(e.Node.Name);
            toolStripComboBox1.Text = e.Node.Name;
        }
        /// <summary>
        /// 大小圖片的轉(zhuǎn)化和引用系統(tǒng)的文件轉(zhuǎn)化圖標
        /// </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
            { }

        }

///引用系統(tǒng)文件獲得系統(tǒng)文件對應的圖標
    [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


 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    麻豆视传媒短视频在线看| 免费在线播放不卡视频| 国产精品久久精品毛片| 亚洲综合伊人五月天中文| 亚洲精品成人综合色在线| 日本高清视频在线观看不卡| 99久只有精品免费视频播放 | 丁香六月婷婷基地伊人| 国产一区二区三区色噜噜| 日本二区三区在线播放| 国产内射一级一片内射高清| 极品少妇嫩草视频在线观看| 中文字幕一区二区三区中文| 国产又粗又深又猛又爽又黄| 免费特黄一级一区二区三区| 国产内射在线激情一区| 中文字幕一区二区熟女| 日韩美女偷拍视频久久| 国产精品二区三区免费播放心 | 日韩欧美中文字幕av| 白白操白白在线免费观看 | 国产免费成人激情视频| 国产精品乱子伦一区二区三区| 91在线爽的少妇嗷嗷叫| 欧美乱码精品一区二区三| 又黄又色又爽又免费的视频| 青草草在线视频免费视频| 五月婷婷综合激情啪啪| 亚洲最新av在线观看| 人妻少妇久久中文字幕久久| 手机在线不卡国产视频| 免费福利午夜在线观看| 成人免费在线视频大香蕉| 91欧美亚洲视频在线| 亚洲另类女同一二三区| 麻豆果冻传媒一二三区| 国产精品欧美激情在线播放| 国产亚洲不卡一区二区| 樱井知香黑人一区二区| 日韩人妻av中文字幕| 九九九热视频免费观看|