<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C#獲取文件夾下的所有文件

          共 4787字,需瀏覽 10分鐘

           ·

          2021-04-29 22:00

           1、獲得當(dāng)前運(yùn)行程序的路徑

           string rootPath = Directory.GetCurrentDirectory();

            2、獲得該文件夾下的文件,返回類型為FileInfo

           string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path); FileInfo[] files=root.GetFiles();

            3、獲得該文件夾下的子目錄,返回類型為DirectoryInfo

           string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path); DirctoryInfo[] dics=root.GetDirectories();

            4、獲得文件夾名

           string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path);string dicName=root.Name;

            5、獲得文件夾完整的路徑名

           string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path); string dicName=root.FullName;

            6、獲取文件的Name和FullName


           string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path);foreach (FileInfo f in root.GetFiles()){    string name=f.Name;    string fullName=f.FullName;


          #只獲取目錄下一級(jí)的文件夾與文件



           String path = @"X:\xxx\xxx";  //第一種方法  string[] files = Directory.GetFiles(path, "*.txt");               foreach (string file in files) {      Console.WriteLine(file);  }   //第二種方法 DirectoryInfo folder = new DirectoryInfo(path);              foreach (FileInfo file in folder.GetFiles("*.txt")) {     Console.WriteLine(file.FullName); }


          # 遞歸地輸出當(dāng)前運(yùn)行程序所在的磁盤下的所有文件名和子目錄名


           1         static void Main(string[] args)
          2 {
          3 //獲取當(dāng)前程序所在的文件路徑
          4 String rootPath = Directory.GetCurrentDirectory();
          5 string parentPath = Directory.GetParent(rootPath).FullName;//上級(jí)目錄
          6 string topPath = Directory.GetParent(parentPath).FullName;//上上級(jí)目錄
          7 StreamWriter sw = null;
          8 try
          9 {
          10 //創(chuàng)建輸出流,將得到文件名子目錄名保存到txt中
          11 sw = new StreamWriter(new FileStream("fileList.txt", FileMode.Append));
          12 sw.WriteLine("根目錄:" + topPath);
          13 getDirectory(sw, topPath, 2);
          14 }
          15 catch (IOException e)
          16 {
          17 Console.WriteLine(e.Message);
          18 }
          19 finally
          20 {
          21 if (sw != null)
          22 {
          23 sw.Close();
          24 Console.WriteLine("完成");
          25 }
          26 }
          27
          28 }
          29
          30 /// <summary>
          31 /// 獲得指定路徑下所有文件名
          32 /// </summary>
          33 /// <param name="sw">文件寫入流</param>
          34 /// <param name="path">文件寫入流</param>
          35 /// <param name="indent">輸出時(shí)的縮進(jìn)量</param>
          36 public static void getFileName(StreamWriter sw, string path, int indent)
          37 {
          38 DirectoryInfo root = new DirectoryInfo(path);
          39 foreach (FileInfo f in root.GetFiles())
          40 {
          41 for (int i = 0; i < indent; i++)
          42 {
          43 sw.Write(" ");
          44 }
          45 sw.WriteLine(f.Name);
          46 }
          47 }
          48
          49 /// <summary>
          50 /// 獲得指定路徑下所有子目錄名
          51 /// </summary>
          52 /// <param name="sw">文件寫入流</param>
          53 /// <param name="path">文件夾路徑</param>
          54 /// <param name="indent">輸出時(shí)的縮進(jìn)量</param>
          55 public static void getDirectory(StreamWriter sw, string path, int indent)
          56 {
          57 getFileName(sw, path, indent);
          58 DirectoryInfo root = new DirectoryInfo(path);
          59 foreach (DirectoryInfo d in root.GetDirectories())
          60 {
          61 for (int i = 0; i < indent; i++)
          62 {
          63 sw.Write(" ");
          64 }
          65 sw.WriteLine("文件夾:" + d.Name);
          66 getDirectory(sw, d.FullName, indent + 2);
          67 sw.WriteLine();
          68 }
          69 }


          往期精彩回顧




          【推薦】.NET Core開發(fā)實(shí)戰(zhàn)視頻課程 ★★★

          .NET Core實(shí)戰(zhàn)項(xiàng)目之CMS 第一章 入門篇-開篇及總體規(guī)劃

          【.NET Core微服務(wù)實(shí)戰(zhàn)-統(tǒng)一身份認(rèn)證】開篇及目錄索引

          Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)

          .NET Core中的一個(gè)接口多種實(shí)現(xiàn)的依賴注入與動(dòng)態(tài)選擇看這篇就夠了

          10個(gè)小技巧助您寫出高性能的ASP.NET Core代碼

          用abp vNext快速開發(fā)Quartz.NET定時(shí)任務(wù)管理界面

          在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務(wù)輕松實(shí)現(xiàn)作業(yè)調(diào)度

          現(xiàn)身說法:實(shí)際業(yè)務(wù)出發(fā)分析百億數(shù)據(jù)量下的多表查詢優(yōu)化

          關(guān)于C#異步編程你應(yīng)該了解的幾點(diǎn)建議

          C#異步編程看這篇就夠了


          瀏覽 115
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产激情无码毛片久久 | 自拍偷拍第12页 | 99偷拍| 美国在线成人 | 不卡高清中文字幕 |