And here is an example of how to get all files (only names if you want) from a directory + files in all sub directory (change the file path):
class Program
{
static void Main(string[] args)
{
List<string> allFiles = GettingFiles(@"C:\1");
}
public static List<string> GettingFiles(string path)
{
List<string> listFiles = new List<string>();
//1. get files from the current directory:
string[] currentFiles = Directory.GetFiles(path, "*.*");
foreach (string file in currentFiles)
listFiles.Add(file);
//2. get files from other sub directories:
string[] directories = Directory.GetDirectories(path);
foreach (string dir in directories)
{
string[] files = GetFilesFromDirectory(dir);
listFiles.AddRange(files);
}
//for the end, lets get only the names of the files (remove the path):
for (int i = 0; i < listFiles.Count; i++)
{
string fileName = Path.GetFileName(listFiles[i]);
listFiles[i] = fileName;
}
return listFiles;
}
private static string[] GetFilesFromDirectory(string path)
{
string[] files = Directory.GetFiles(path);
return files;
}
}
btw, thx ddanbe :)
Hope it helps,
Mitja