0
Could I update a specific line of the CSV file? i have the information of which line I want to add information, currently I have the code that goes through the file and when it arrives in the line I want it updates the line and continues writing the rest of the lines, however this is causing me a loss of performasse, because my file is very large, if someone knows how to edit using some parameter to go straight to the line as if it were an array?
public string EscreveCSV(string caminho, int linha, string mensagem)
{
//Só continua se o arquivo informado existir
if (File.Exists(caminho))
{
//lista que irá armazenar cada linha do arquivo
List<string> linhas = new List<string>();
using (TextReader tr = new StreamReader(caminho, Encoding.Default))
{
string sLinha = null;
while ((sLinha = tr.ReadLine()) != null)
{
linhas.Add(sLinha); //adiciona cada linha do arquivo à lista
}
tr.Close();
tr.Dispose();
}
//Só continua se o arquivo ter um número maior de linhas informadas
if (linhas.Count > linha)
{
linhas[linha] += ";" + mensagem; //adiciona a mensagem informada ao final da linha
using (TextWriter tw = new StreamWriter(caminho, false, Encoding.Default))
{
foreach (string l in linhas)
{
tw.WriteLine(l); //escreve no arquivo novamente
}
tw.Close();
}
return "Arquivo Escrito com sucesso"; //mensagem de retorno
}
else
{
return "Arquivo Não possui a linha informada";
}
}
else
{
return "Arquivo não existe";
}
}
Post the current code
– novic
Ready @Virgilionovic
– Jhonatas Silva
Take a look here https://superuser.com/questions/7169/querying-a-csv-file
– Denis
@Denis, I looked but did not find how it could help me and I believe he is not using the C#
– Jhonatas Silva
There is a way to pick up all the lines, then make the check after recording again, maybe (without testing has no way to say if it is more performatic) because, if you do the manual process is should be very time consuming
– novic
CSV is a text file, unless the new line has exactly the same number of characters as the one being overwritten it has no way to change without recreating the entire file
– Leandro Godoy Rosa
@Virgilionovic I’ll see if I can do something following that line of reasoning
– Jhonatas Silva
@Leandrogodoyrosa would not replace but add information at the end of the line
– Jhonatas Silva
@Jhonatassilva I’m going to propose a test sketch and tell me what happened if it was more performatic.
– novic
@Jhonatassilva how many lines your file has?
– novic
@Virgilionovic I am trying with a file that has 206 columns and 300 lines but I do validation in all fields/ line, then when I find an error this validation call the method that writes this error in CSV
– Jhonatas Silva
@Jhonatassilva is not better to open the file in memory, check the error, change, and then until it’s over and write everything!?
– novic
Memory manipulation is always better, unless to make a loose change and close right after
– Isac
I thank everyone for the comment, it was very valid, I managed to solve the problem, instead of reading and recording in the spreadsheet (CSV) all the time and I threw the information in a list, I do all the manipulation of the data I need and only at the end write in CSV, much increase to performasse, I will put here share the code that lists the spreadsheet initially, and what updates in the lines I need.
– Jhonatas Silva