C#フォルダの下にあるすべてのファイルを取得
C#フォルダの下にあるすべてのファイルを取得
現在の実行プログラムが存在するディスクの下にあるすべてのファイル名とサブディレクトリ名を再帰的に出力します。
#基礎知識1、現在実行中のプログラムのパスを取得する1 string rootPath = Directory.GetCurrentDirectory();
2、このフォルダの下のファイルを取得し、戻りタイプはFileInfo1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 FileInfo[] files=root.GetFiles();
3、このフォルダの下のサブディレクトリを取得し、返却タイプはDirectoryInfoである1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 DirctoryInfo[] dics=root.GetDirectories();
4、フォルダ名を取得1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.Name;
5、フォルダの完全なパス名を取得する1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.FullName;
6、ファイルを取得するNameとFullName1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 foreach (FileInfo f in root.GetFiles())
4 {
5 string name=f.Name;
6 string fullName=f.FullName;
7 }
#ディレクトリの次のレベルのフォルダとファイルのみを取得 1 String path = @"X:\xxx\xxx";
2
3 //
4 string[] files = Directory.GetFiles(path, "*.txt");
5
6 foreach (string file in files)
7 {
8 Console.WriteLine(file);
9 }
10
11 //
12 DirectoryInfo folder = new DirectoryInfo(path);
13
14 foreach (FileInfo file in folder.GetFiles("*.txt"))
15 {
16 Console.WriteLine(file.FullName);
17 }
現在の実行プログラムが存在するディスクの下にあるすべてのファイル名とサブディレクトリ名を再帰的に出力します。
1 static void Main(string[] args)
2 {
3 //
4 String rootPath = Directory.GetCurrentDirectory();
5 string parentPath = Directory.GetParent(rootPath).FullName;//
6 string topPath = Directory.GetParent(parentPath).FullName;//
7 StreamWriter sw = null;
8 try
9 {
10 // , 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 ///
31 ///
32 ///
33 ///
34 ///
35 ///
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 ///
50 ///
51 ///
52 ///
53 ///
54 ///
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 }
1 string rootPath = Directory.GetCurrentDirectory();
1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 FileInfo[] files=root.GetFiles();
1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 DirctoryInfo[] dics=root.GetDirectories();
1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.Name;
1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.FullName;
1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 foreach (FileInfo f in root.GetFiles())
4 {
5 string name=f.Name;
6 string fullName=f.FullName;
7 }
1 String path = @"X:\xxx\xxx";
2
3 //
4 string[] files = Directory.GetFiles(path, "*.txt");
5
6 foreach (string file in files)
7 {
8 Console.WriteLine(file);
9 }
10
11 //
12 DirectoryInfo folder = new DirectoryInfo(path);
13
14 foreach (FileInfo file in folder.GetFiles("*.txt"))
15 {
16 Console.WriteLine(file.FullName);
17 }
1 static void Main(string[] args)
2 {
3 //
4 String rootPath = Directory.GetCurrentDirectory();
5 string parentPath = Directory.GetParent(rootPath).FullName;//
6 string topPath = Directory.GetParent(parentPath).FullName;//
7 StreamWriter sw = null;
8 try
9 {
10 // , 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 ///
31 ///
32 ///
33 ///
34 ///
35 ///
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 ///
50 ///
51 ///
52 ///
53 ///
54 ///
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 }