C# 编写文件资源浏览器

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

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

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

C# 编写文件资源浏览器

 

源程序代码

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);
            //}
        }
    }
}

 

上一篇:微信⼩程序开发 python+vue+django+drf框架


下一篇:.net程序设计文件操作