统计文件类型和个数

1.全局变量
        int totalFile = 0;
        Hashtable list = new Hashtable();
2.form控件
 txt_total
 txt_path
 lbx_filetype
 btn_search
3.按钮函数
        private void btn_search_Click(object sender, EventArgs e)
        {

            string FilePath = txt_path.Text;

            if (FilePath != "")
            {
                totalFile = 0;
                txt_total.Text = "";
                lbx_filetype.Items.Clear();
                list.Clear();
                //int TotalCount = GetFilesCount(FilePath);
                //txt_total.Text = "总个数:" + TotalCount.ToString() + " ";
                FindFile(FilePath);
                txt_total.Text += "总计" + totalFile.ToString() + "个文件 ";
                foreach (DictionaryEntry item in list)
                {
                    txt_total.Text += string.Format("{0}:{1}个  ", item.Key, item.Value);
                }
            }
            else
            {
                MessageBox.Show(lbx_filetype, "请输入文件夹路径");
            }
        }
4.查询函数(递归)
        public void FindFile(string dirPath)
        {
            //在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
            DirectoryInfo Dir = new DirectoryInfo(dirPath);
            string Split = "";
            try
            {
                foreach (FileInfo f in Dir.GetFiles("*")) //查找文件
                {
                    lbx_filetype.Items.Add(Dir + Split + f.ToString());
                    totalFile++;
                    string suffix = f.ToString().Split('.').Last().ToString();
                    if (list.ContainsKey(suffix))
                    {
                        int c = (Int32)list[suffix];
                        list[suffix] = c + 1;
                    }
                    else
                    {
                        list.Add(suffix, 1);
                    }
                }
                foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
                {
                    FindFile(Dir + Split + d.ToString());
                    lbx_filetype.Items.Add(Dir + Split + d.ToString());
                }

            }

            catch (Exception e)
            {

                MessageBox.Show(e.Message);

            }

        }
4.查询函数(不递归)
        public int GetFilesCount(string FilePath)
        {
            int totalFile = 0;
            string[] files = Directory.GetFiles(FilePath, "*", SearchOption.AllDirectories);
            totalFile = files.Length;
            //string[] fileList = Directory.GetDirectories(FilePath);
            //foreach (string fileChild in fileList)
            //{
            //    totalFile += GetFilesCount(fileChild);

            //}
            for (int i=0; i < totalFile;i++ )
            {
                lbx_filetype.Items.Add(files[i].ToString());
                string suffix = files[i].ToString().Split('.').Last().ToString();
                if (list.ContainsKey(suffix))
                {
                    int c = (Int32)list[suffix];
                    list[suffix] = c + 1;
                }
                else
                {
                    list.Add(suffix, 1);
                }
            }
            return totalFile;
        }

by 雪洁 2011-06-07 15:37:48 2694 views
我来说几句

相关文章