c# CSV text file to single string (or array or list ) then add to or alter each string and output to single string variable -
i using c# datagridview multiple rows (which may vary based on user input ) export plain text file result :
compname,poweron,thin,storage,acceptlic,verify,ssl,c:\test\myfile.img,root,p@ssw0rd,192.168.1.100 compname,poweron,thin,storage,acceptlic,verify,ssl,c:\test\myfile.img,root,p@ssw0rd,192.168.1.100 compname,poweron,thin,storage,acceptlic,verify,ssl,c:\test\myfile.img,root,p@ssw0rd,192.168.1.100 //...and more lines if user adds more
what parse file , load each line single variable append command line executable example:
"c:\program files\mytest\mytest tool\mytest.exe" -n=compname --poweron -dmode=convert -volume=storage1 --acceptlicense --noverification --ssl c:\users\me\documents\down\myfile.img system1://root:mypassword@192.168.1.151
so question how done ?
i have checked many examples , it's starting rather confusing method use produce required result. (streamreader, stringsplit,stringjoin , more..)
i able contents of file build list using :
list<string> list = new list<string>(); using (streamreader reader = new streamreader(@"c:\foo\stest.txt")) { string line; while ((line = reader.readline()) != null) { list.add(line); // add list. console.writeline(line); // write console. stringbuilder builder = new stringbuilder(); //but here ? }
am on right track ? appreciate answrs ..
thank kindly
i write messy logic here you, please check , correct if assumptions wrong.
list<string> list = new list<string>(); using (streamreader reader = new streamreader(@"c:\projects\consoleapplication1\consoleapplication1\bin\debug\txt1.txt")) { string line; string[] parameters = null; while ((line = reader.readline()) != null) { list.add(line); // add list. console.writeline(line); // write console. stringbuilder builder = new stringbuilder(); parameters = line.split(','); builder.append("-n="); int paramcount = 0; foreach (var param in parameters) { if (paramcount == 0) { builder.append(param + " "); } else if (paramcount == 2) { // req seems need static parameter here // if assumption wrong, correct logic builder.append(string.format("-dmode=convert ")); } else if (paramcount == 3) { // req seems need static parameter here // if assumption wrong, correct logic builder.append(string.format("-volume=storage1 ")); } else if(paramcount <= 5 ) { builder.append(string.format("--{0} ", param)); } else if (paramcount > 5) { //correct logic per requirement. string sslparams = " --" + param + " "; string[] sslparamvalues = parameters.take(11).skip(7).toarray(); sslparams += string.join(",", sslparamvalues); builder.append(string.format("--{0} ", sslparams)); } paramcount++; } console.writeline(builder.tostring()); process p = new process(); p.startinfo.filename = @"c:\program files\mytest\mytest tool\mytest.exe"; p.startinfo.arguments = string.format(builder.tostring()); //p.start();
Comments
Post a Comment