c#用递归方法在treeView中实现文件夹目录的查看及文件打开——递归学习用例

//第一次发博客。希望有热心的前辈可以给我提出建议。编码还不够成熟让大家见笑了!



    public partial class Form1 : Form
    {
        #region 定义变量
        int i = 0, j = 0;//定义两个整数用来计算文件夹和文件的数量
        public static int num;//定义一个整型静态变量来控制加载深度
        public static string path;//定义一个字符串变量来表示完整路径
        public static List<string> list = new List<string>();//保存已加载的文件夹
        string strPath = "";//最初加载路径
        string strCrTime, strLsTime, strLWTime, strName, strFName, strDName, strlSRead;
        string flength;
        #endregion

        #region 初始化窗体
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region 递归算法 遍历文件夹 如果遍历的结果是文件夹 则继续遍历并添加节点
        TreeNode AddNode(FileSystemInfo fsinfo)
        {
            num++;
            //创建一个新的节点为文件
            TreeNode node = new TreeNode(fsinfo.Name);
            if (num < 2)
            {
                #region 判断是否为文件夹如果不是则返回
                //判断文件是否是文件夹
                if (fsinfo is DirectoryInfo)
                {
                    if (!list.Contains(fsinfo.Name))
                    {
                        list.Add(fsinfo.Name);
                    }
                    j++;
                    //如果文件是文件夹,则转换为文件夹类型
                    DirectoryInfo dinfo = fsinfo as DirectoryInfo;
                    try
                    {
                        //继续遍历文件夹
                        foreach (FileSystemInfo fsinfo1 in dinfo.GetFileSystemInfos())
                        {
                            node.Nodes.Add(AddNode(fsinfo1));
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally { }
                }
                else { i++; }
                #endregion
            }
            else { return node; }
            return node;
        }
        #endregion

        #region 选择路径
        private void btnRead_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();//清空treeview
            //判断文件夹对话框是否打开
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                //将选择的文件夹路径填写到文本框
                tbxchoosFold.Text = folderBrowserDialog1.SelectedPath;
                //创建一个文件夹对象
                DirectoryInfo dinfo = new DirectoryInfo(tbxchoosFold.Text);
                //创建一个文件数组
                FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
                //遍历文件夹 并将文件名附加到树节点
                foreach (FileSystemInfo fsinfo in fsinfos)
                {
                    num = 0;
                    treeView1.Nodes.Add(AddNode(fsinfo));
                }
            }
            strPath = tbxchoosFold.Text;
            //将统计的数量填写到文本框
            tbxWJ.Text = i.ToString();
            tbWJj.Text = j.ToString();
        }
       
        #endregion

        #region 递归算法获得当前选择的绝对路径
        TreeNode getNode(TreeNode getNode1)
        {
            TreeNode getNode2 = getNode1.Parent;
            path = getNode2.Text + "\\" + path;
            if (getNode2 == null)
            {
                return getNode2;
            }
            if (getNode2.Parent != null)
            {
                getNode(getNode2);
                return getNode2;

            }
            else { return getNode2; }
        }
       
        #endregion

        #region 选择事件加载
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lbstatus.Text = string.Empty;
            path = "";

            // path = e.Node.Text;
            TreeNode chooseNode = treeView1.SelectedNode;
            path = chooseNode.Text;
            //判断是否为空项
            if (chooseNode == null)
            {
                return;
            }
            if (chooseNode.Parent != null)
            {
                if (!list.Contains(chooseNode.Parent.Text))
                {
                    list.Add(chooseNode.Parent.Text);
                }
                //计入递归方法获取完整路径
                getNode(chooseNode);
            }
            path = strPath + path;
            tbxchoosFold.Text = path;

            //创建一个文件夹对象
            DirectoryInfo dinfo = new DirectoryInfo(path);

            try
            {
                //创建一个文件数组
                FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
                if (fsinfos.Length == 0)
                {
                    lbstatus.Text = "文件夹为空!";
                    lbstatus.ForeColor = Color.Red;
                    return;
                }
                else
                {
                    //在首次加载的时候,已经加载到第二层文件,如果点击首层文件夹则不扫描文件夹
                    if (chooseNode.Parent == null)
                    {
                        return;
                    }
                    if (list.Contains(chooseNode.Text) && chooseNode.Nodes.Count != 0)
                    {
                        return;
                    }

                    //遍历文件夹 并将文件名附加到树节点
                    foreach (FileSystemInfo fsinfo in fsinfos)
                    {
                        num = 0;
                        chooseNode.Nodes.Add(AddNode(fsinfo));
                    }
                    tbxWJ.Text = i.ToString();
                    tbWJj.Text = j.ToString();
                }
            }
            catch (Exception)
            {
                if (path.Contains("."))
                {
                    try
                    {
                        FileInfo fie = new FileInfo(path);
                        DialogResult result;
                        strCrTime = fie.CreationTime.ToShortDateString();
                        strLsTime = fie.LastAccessTime.ToShortDateString();
                        strLWTime = fie.LastWriteTime.ToShortDateString();
                        strName = fie.Name;
                        strFName = fie.FullName;
                        strDName = fie.DirectoryName;
                        strlSRead = fie.IsReadOnly.ToString() == "true" ? "是" : "否";
                        if (fie.Length / 1024 < 1)
                        {
                            flength = fie.Length.ToString() + "b";
                        }
                        else if (fie.Length / 1024 < 1024&&fie.Length/1024>1)
                        {
                            flength = (fie.Length / 1024).ToString() + "kb";
                        }
                        else
                        {
                            flength = (fie.Length / 1024576).ToString() + "Mb";
                        }

                        if (path.Contains("exe"))
                        {
                            result = MessageBox.Show("是否打开程序?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                        }
                        else if (path.Contains("zip") || path.Contains("rar"))
                        {
                            result = MessageBox.Show("是否打开压缩文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                        }
                        else
                        {
                            result = MessageBox.Show("是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                        }
                        if (result == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(path);
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally { }
                }
                // MessageBox.Show("文件不能用作文件夹打开!");

            }
            finally
            {

            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("文件信息:\n创建时间:"+strCrTime+"\n上次访问时间:"+strLsTime+"\n上次写入时间:"+strLWTime+"\n文件名称:"+strName+"\n完整目录:"+strFName+"\n完整路径:"+strDName+"\n是否只读:"+strlSRead+"\n文件长度:"+flength);
        }
    }

posted @ 2014-08-18 20:34  不再奢望  阅读(1046)  评论(0)    收藏  举报