Net Core教程

C# 编写文件资源浏览器

本文主要是介绍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# 编写文件资源浏览器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!