hello,
i have this code
DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\lab"); // or whatever it's called
FileInfo[] fia = di.GetFiles();
DirectoryInfo[] subDirs = di.GetDirectories();
string[] excluded = { "10.bmp", "New Folder1"};
foreach (DirectoryInfo subDir in subDirs)
foreach (FileInfo fi in fia)
{
if (fi.Name == excluded[0] || fi.Name == excluded[1] ) continue;
try
{
fi.Delete();
subDir.Delete(true);
}
catch
{
//
}
}
subDirs = null;
this code wile delete all files and folders except the files i want (10.bmp)
but the problem is i can exclude files but i can't exclude Folders (New Folder1)
can someone HELP ME.

2 answers
You have nested foreach loops so it won't behave as you had intended. You should iterate through all the files then iterate through all the folders in separate loops.
i.e.,
answered one year ago by:
538
153
thank you Jeff1203
in line
you have to add other test for exclusion as you have for file:
if you add so after you will have 2 tests
one for directories and one for files
good luck
answered one year ago by:
556
153
i tried to do that but there are 14 error!