In using C# for desktop application development, there are often cases of operating files. At this time, it may be necessary to scan the folder and obtain all files.
The method is as follows
/// <summary> /// traversal to get all files /// </summary> /// <param name="filePathByForeach"></param> /// <param name="result"></param> public static void ForeachFile(string filePathByForeach, ref string result) { try { DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach); DirectoryInfo[] dirInfo = ();//Get the folder in the directory FileInfo[] file = ();//Get the file in the directory foreach (FileInfo fileItem in file) //Transfer the file { result += + @"\" + + "\n"; } //Transfer the folder foreach (DirectoryInfo NextFolder in dirInfo) { ForeachFile(, ref result); } } catch (Exception) { throw; } }
The above is the detailed content of the sample code for obtaining all files by c# traversing. For more information about c# traversing all files, please pay attention to my other related articles!