i want able log path of files text file. have following logging.
static void logfile(string lockedfilepath) { assembly ass = assembly.getexecutingassembly(); string workingfolder = system.io.path.getdirectoryname(ass.location); string logfile = system.io.path.combine(workingfolder, "logfiles.txt"); if (!system.io.file.exists(logfile)) { using (system.io.filestream fs = system.io.file.create(logfile)) { using (system.io.streamwriter sw = new streamwriter(fs)) { sw.writeline(lockedfilepath); } } } else { using (system.io.filestream fs = system.io.file.openwrite(logfile)) { using (system.io.streamwriter sw = new streamwriter(fs)) { sw.writeline(lockedfilepath); } } } } but if call in console app
foreach (string f in system.io.directory.getfiles(@"c:\aasource")) { console.writeline("logging : " + f); logfile(f); } console.readline(); the file listed in resulting text file last file in dir. doing wrong?
instead of system.io.file.openwrite(logfile), use system.io.file.appendtext(logfile). when use openwrite you're going overwriting contents whatever write in.
additionally, if statement (if (!system.io.file.exists(logfile))) not required. appendtext (and openwrite) matter create file if doesn't exist. means can away running code within else clause.
Comments
Post a Comment