• 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);
                //}
            }
        }
    }
  • 相关阅读:
    使用__autoload()来管理文件导入
    PHP 类中的常量
    PHP 访问类中的静态属性
    PHP 中的静态变量的简单使用
    Vscode下Python的用户界面介绍
    VsCode下代码导航
    在VS代码中编辑Python
    vscode下Python设置参考
    在VS Code中对Python进行单元测试
    解决html5中video标签无法播放mp4问题的办法
  • 原文地址:https://www.cnblogs.com/Arisf/p/15559482.html
Copyright © 2020-2023  润新知