Diese Funktion sucht nach allen Dateien in einem Ordner und optional auch in Unterordnern. Man bekommt die Dateiliste als "List<string>" zurück.
Übernommen von Khartak und entsprechend angepasst.
public class ListFiles
{
/// <summary>
/// Creates a list which contains all filenames in a specific folder
/// </summary>
/// <param name="Root">Folder which contains files to be listed</param>
/// <param name="SubFolders">True for scanning subfolders</param>
/// <returns></returns>
public List<string> GetFileList(string Root, bool SubFolders)
{
List<string> FileArray = new List<string>();
try {
string[] Files = System.IO.Directory.GetFiles(Root);
string[] Folders = System.IO.Directory.GetDirectories(Root);
for (int i = 0; i < Files.Length; i++)
{
FileArray.Add(Files[i].ToString());
}
if (SubFolders == true)
{
for (int i = 0; i < Folders.Length; i++)
{
FileArray.AddRange(GetFileList(Folders[i], SubFolders));
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
return FileArray;
}
}
2 Kommentare zum Snippet