var filenames3 = Directory
.GetFiles(dirPath, "*", SearchOption.AllDirectories)
.Select(f => Path.GetFileName(f));
filenames only
var filenames4 = Directory
.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories)
.Select(Path.GetFileName); // <-- note you can shorten the lambda
file names and directory path.....
// - file1.txt
// - file2.txt
// - subfolder1/file3.txt
// - subfolder2/file4.txt
var skipDirectory = dirPath.Length;
// because we don't want it to be prefixed by a slash
// if dirPath like "C:\MyFolder", rather than "C:\MyFolder\"
if(!dirPath.EndsWith("" + Path.DirectorySeparatorChar)) skipDirectory++;
var filenames4s = Directory
.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories)
.Select(f => f.Substring(skipDirectory));
Try this....
filenames3.SequenceEqual(filenames4).Dump(".NET 3 and 4 methods are the same?");
filenames3.Dump(".NET 3 Variant");
filenames4.Dump(".NET 4 Variant");
filenames4s.Dump(".NET 4, subfolders Variant");
simple linq query..
string[] files = Directory.EnumerateFiles("C:\Something", "*.*")
.Select(p => Path.GetFileName(p))
.Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)).ToArray();
try this also
string filePath = "c:\Public\";
DirectoryInfo apple = new DirectoryInfo(@filepath);
foreach (var file in apple.GetFiles("*")
{
//do the thing
Console.WriteLine(file)
}
Comments
Post a Comment