how to add time to C# console array -
i know how filename array , load filename csv file. example, filename, file_1_20170428101607. want file name (date , time format – 28/04/2017 10:16:07) parse column in output file (csv file). code time, can please check , how add time array parse csv file?
private static string[] getfilenames(string path, string filter) { string filename = @”c:\\desktop\\file_1_20170428101607.csv”; string result; result = path.getfilename(filename); console.writeline(“getfilename(‘{0}’) returns ‘{1}'”, filename, result); string[] files = directory.getfiles(path, filter); (int = 0; parsing not possible -> return null { return null; } } private static datetime? parsedatefromfilename(string filename) { //regex used extract 14 digit timestamp filename var timestamp = regex.match(filename, @"\d{14}$"); if (!timestamp.success) { //if no match found return null return null; } try { //try parse date known timestamp , return return datetime.parseexact(timestamp.value, "yyyymmddhhmmss", cultureinfo.invariantculture); } catch { //any error -> parsing not possible -> return null return null; }
you can try this:
first create stringbuilder
contain csv content:
stringbuilder csv = new stringbuilder(); string filepath = "...";
next create for loop
, take values 2 columns , append stringbuilder
//for loop start string filename = "file_1_20170428101607"; string datepart = filename.split('_').last(); datetime dt = datetime.parseexact(datepart, "yyyymmddhhmmss", null); string datestring = dt.tostring("dd/mm/yyyy hh:mm:ss", system.globalization.cultureinfo.invariantculture); var firstcolval = filename; var secondcolval = datestring; var newline = string.format("{0},{1}", firstcolval, secondcolval); csv.appendline(newline); //loop end
after for loop
save stringbuilder
content csv file:
file.writealltext(filepath, csv.tostring());
Comments
Post a Comment