.net - Writing huge amount of physical file names into text files c# -
i have developed small tool used display data discrepancy in c#, explained point wise below,
- fetch data database , write list of file names in text file based on date criteria -output 1
- take dir of path 1 , write text file- output2
- take dir of path2, path3 , path4 , write text files separately each path- output 3/4/5
- compare option: compare output1 , 2 , write down difference in text file, difference compared output3 , again difference written in file, , on...
- my issue : last path has more 2.5 million records of files, whenever try writing in text file hangs application , never provides output, did try filtering date criteria single day records around 30 thousands hangs
i have searched many sites did not solution can understand or able implement it. below attempted code.
if (!txtpath3.text.equals(string.empty) && system.io.directory.getfiles(txtpath3.text).length > 0) { var directory = txtpath3.text; var from_dt = this.dtpickerstart.value; var end_dt = this.dtpickerend.value; datetime from_date = from_dt; datetime to_date = end_dt; directoryinfo di = new directoryinfo(directory); filesysteminfo[] files = di.getfilesysteminfos(); var op = di.getfiles() .where(file => file.lastwritetime >= from_date && file.lastwritetime <= to_date); foreach (string file in system.io.directory.getfiles(txtpath3.text, "*.*")) { textwriter tw = new streamwriter(dirfile3, true); tw.writeline("" + file + ""); tw.close(); } } else { }
your foreach
-loop opens , closes file lines. should open , close file outside of loop.
using(var tw = new streamwriter(dirfile3, true)) { foreach (string file in system.io.directory.getfiles(txtpath3.text, "*.*")) { tw.writeline("" + file + ""); } }
even easier using existing functions this:
file.appendalllines(dirfile3, system.io.directory.getfiles(txtpath3.text, "*.*"));
as 2.5 million filesnames lot keep in ram @ same time, might better off enumerating them:
file.appendalllines(dirfile3, system.io.directory.enumeratefiles(txtpath3.text, "*.*"));
Comments
Post a Comment