zoukankan      html  css  js  c++  java
  • C# 编写文件资源浏览器

    使用C#创建Windows窗体程序,实现文件资源浏览

    1. 创建Windows窗体程序
    2. 将Form的Text属性设置为“简单的文件管理器”
    3. 在窗体上添加SplitContainer、TreeView、ListView控件
      • TreeView控件的Dock属性设置为Left

      • ListView控件的Dock属性设置为Fill,MultiSelect属性设置为False,View属性设置为List
    4. 窗体界面

    源程序代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace sy4_1
    {
        public partial class Form1 : Form
        {
            //Form1构造函数
            public Form1()
            {
                InitializeComponent();
                TreeNode myComputerNode = new TreeNode("我的电脑");
                treeView1.Nodes.Add(myComputerNode);
                listViewShow(myComputerNode);
            }
    
            //控制ListView控件显示
            private void listViewShow(TreeNode dirNode)
            {
                listView1.Clear();
                if (dirNode.Parent == null)
                {
                    foreach (string DrvName in Directory.GetLogicalDrives())
                    {
                        ListViewItem aItem = new ListViewItem(DrvName);
                        listView1.Items.Add(aItem);
                    }
                }
                else
                {
                    foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))
                    {
                        ListViewItem aItem = new ListViewItem(DirName);
                        listView1.Items.Add(aItem);
                    }
                    foreach (string fileName in Directory.GetFiles((string)dirNode.Tag))
                    {
                        ListViewItem aItem = new ListViewItem(fileName);
                        listView1.Items.Add(aItem);
                    }
                }
    
            }
    
            //listViewShow方法重载
            private void listViewShow(string dirName) 
            {
                listView1.Clear();
                try
                {
                    foreach (string DirName in Directory.GetDirectories(dirName))
                    {
                        ListViewItem aItem = new ListViewItem(DirName);
                        listView1.Items.Add(aItem);
                    }
                    foreach (string fileName in Directory.GetFiles(dirName))
                    {
                        ListViewItem aItem = new ListViewItem(fileName);
                        listView1.Items.Add(aItem);
                    }
                }
                catch 
                {
                }
            }
    
            //控制TreeView控件显示
            private void DirTreeShow(TreeNode dirNode)
            {
                try
                {
                    if (dirNode.Nodes.Count == 0)
                    {
                        if (dirNode.Parent == null)
                        {
                            foreach (string DrvName in Directory.GetLogicalDrives())
                            {
                                TreeNode aNode = new TreeNode(DrvName);
                                aNode.Tag = DrvName;
                                dirNode.Nodes.Add(aNode);
                            }
                        }
                        else
                        {
                            foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))
                            {
                                TreeNode aNode = new TreeNode(DirName);
                                aNode.Tag = DirName;
                                dirNode.Nodes.Add(aNode);
                            }
                        }
                    }
                }
                catch 
                {
                }
            }
    
            //TreeView控件点击事件
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                listViewShow(e.Node);
                DirTreeShow(e.Node);
            }
    
    
            //ListView双击事件
            private void listView1_DoubleClick(object sender, EventArgs e)
            {
                foreach (int listIndex in listView1.SelectedIndices)
                {
                    listViewShow(listView1.Items[listIndex].Text);
                }
            }
    
            //ListView单击事件
            private void listView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //foreach (int listIndex in listView1.SelectedIndices)
                //{
                //    listViewShow(listView1.Items[listIndex].Text);
                //}
            }
        }
    }
  • 相关阅读:
    C语言变长数组data[0]总结
    常见网络摄像机默认使用的端口,RTSP地址
    目前使用过的各大厂商rtsp取流的url
    Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu
    笔记整理--C语言
    笔记整理--LibCurl开发
    Linux下Socket连接超时的一种实现方法(转载)
    笔记整理--Linux守护进程
    笔记整理--Linux编程
    笔记整理--Linux平台MYSQL的C语言
  • 原文地址:https://www.cnblogs.com/Arisf/p/15559482.html
Copyright © 2011-2022 走看看