日期:2014-05-17  浏览次数:21072 次

怎么遍历文件夹下面所有文件?

  foldPath = dialog.SelectedPath;
                this.label4.Text = foldPath;
                
                DirectoryInfo TheFolder = new DirectoryInfo(foldPath);
                fileName = Path.GetFileName(dialog.SelectedPath);//获取当前打开文件夹名称
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    this.listBox1.Items.Add(NextFile.Name);
                    string tempStr = File.ReadAllText(NextFile.FullName, Encoding.GetEncoding("GB2312"));//读取txt内容 
                    this.richTextBox1.Text = tempStr;
                }

这个方法就只能遍历第一个文件的所有内容,我用listBox1显示出了所有的文件名称,请问下要怎么遍历全部文件呢?
------解决方案--------------------
http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html
------解决方案--------------------

        private void ScanAllFolder(string pathName)
         {
             try
             {
                 if (!Directory.Exists(pathName))
                 {
                     return;
                 }
 
                 DirectoryInfo processDirectory = new DirectoryInfo(pathName);
 
                 FileSystemInfo[] fileSystemInfos = processDirectory.GetFileSystemInfos();
 
                 foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
                 {
                     if (fileSystemInfo is DirectoryInfo &&