如何获取文件夹下各子文件夹名,同时将这些信息输出到文本文件中呢?求赐教!...

本文介绍了一个使用C#进行目录遍历并获取所有子文件夹名称的方法,同时展示了如何将这些名称写入到指定文件的过程。该示例通过具体的代码实现,帮助读者理解C#中文件和目录操作的基本用法。

using System;

using System.Collections.Generic;

using System.Linq; using System.Data;

using System.Configuration;

using System.Collections;

using System.Net;

using System.Text;

using System.IO;

using System.Text.RegularExpressions;

using System.Diagnostics;

namespace zheng_li

{     class Program  

   {       

  static void Main(string[] args)    

     { 
        string path = @"F:\data_all\DaZhongDianPing\BeiJing_dazhong\DaZhongDianPing_yonghu\bin\x86\Debug\用户点评";///根文件夹地址   
          DirectoryInfo dif = new DirectoryInfo(path);///文件夹实例        

         FileSystemInfo[]fsis = dif.GetFileSystemInfos();////获取子文件夹          

        string path1 = @"F:\data_all\ce_shi.txt";///定义新的文件路径            

        FileInfo f1 = new FileInfo(path1);           

       f1.Create();///创建文件        

       int count=0;               

  foreach (FileSystemInfo fsi in fsis)            

     {      DirectoryInfo dif1 = new DirectoryInfo(fsi.FullName);              

           string str = dif1.Name;///子文件夹名                    

           Console.WriteLine(str);                       

           FileStream fs = new FileStream(path1, FileMode.Open,FileAccess.ReadWrite);                       

          StreamWriter sr = new StreamWriter(fs);                     

          sr.Write(str);///流写入创建的文件中                        

          count += 1;   //// 记数,一共多少个子文件夹                           

     if(count>7254)             

        { sr.Close();                     

          fs.Close();///记数超过子文件夹数后,停止写入关闭流;               

      }                                    

}      

   }   

  }

}

转载于:https://www.cnblogs.com/Lxiaojiang/p/3568781.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值