c# - Generate list of files in directory with relative path -
so, i'm making little app generates list of files inside folder (recursive) command line argument. generate file list relative paths can use them later. command argument folder c:/folder, want inside folder not including folder itself.
this now
c:/folder\some.exe c:/folder\locales\en.pak c:/folder\logs\client_2016.log this like.
some.exe locales\en.pak logs\client_2016.log code
public static void processdirectory(string targetdirectory) { // process list of files string[] fileentries = directory.getfiles(targetdirectory); parallel.foreach(fileentries, fileentry => { processfile(fileentry); }); // recurse subdirectories string[] subdirectoryentries = directory.getdirectories(targetdirectory); parallel.foreach(subdirectoryentries, subdirectoryentry => { processdirectory(subdirectoryentry); }); } public static void processfile(string path) { //string output = path.replace(environment.getcommandlineargs, ""); maybe? //console.writeline(output); console.writeline(path); }
you don't need handle recursion yourself; can use overload of directory.enumeratefiles() takes searchoption parameter.
provided root starts drive letter or \\, can want this:
string root = @"d:\test\"; var files = directory.enumeratefiles(root, "*", searchoption.alldirectories) .select(path => path.replace(root, "")); // files contains file names want, root removed. console.writeline(string.join("\n", files)); note file entries not fetched until enumerate them. can put them in list so:
var listoffiles = files.tolist(); or can use foreach iterate on them without putting them in list first.
foreach (var file in files) { // file. }
Comments
Post a Comment