以前在撰寫某某工具時所撰寫的程式,遞迴找出某個目錄下的所有檔案,並確認其狀態
以前在撰寫某某工具時所撰寫的程式,遞迴找出某個目錄下的所有檔案,並確認其狀態:
01
/// <summary>
02
/// 找出某目錄下的所有檔案
03
/// </summary>
04
/// <param name="sDir">目錄路徑</param>
05
public void DirSearch(string sDir)
06
{
07
try
08
{
09
//先找出所有目錄
10
foreach (string d in Directory.GetDirectories(sDir))
11
{
12
//先針對目前目路的檔案做處理
13
foreach (string f in Directory.GetFiles(d))
14
{
15
isFileCheckOut(f);
16
}
17
//此目錄處理完再針對每個子目錄做處理
18
DirSearch(d);
19
}
20
}
21
catch (System.Exception excpt)
22
{
23
Console.WriteLine(excpt.Message);
24
}
25
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

然後下面這個是在網路上查到別人寫的一個檔案複製程式,適合用在檔案的備份,寫法上跟上頭的其實大同小異,邏輯上應該是互通的。
01
private void CopyDir(string srcPath, string aimPath)
02
{
03
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
04
{
05
aimPath += Path.DirectorySeparatorChar;
06
}
07
08
if (!Directory.Exists(aimPath))
09
{
10
Directory.CreateDirectory(aimPath);
11
}
12
string[] fileList = Directory.GetFileSystemEntries(srcPath);
13
foreach (string file in fileList)
14
{
15
if (Directory.Exists(file))
16
{
17
CopyDir(file, aimPath + Path.GetFileName(file));
18
}
19
else
20
{
21
File.Copy(file, aimPath + Path.GetFileName(file), true);
22
}
23
}
24
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

![]() |
游舒帆 (gipi) 探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。 |