How to break line using Streamwriter?

Asked

Viewed 486 times

0

I have this code that generates a log always when the service ends, but it can happen to have unprocessed NF and I need to list it to be easy to identify.

Example 100 NF were not processed. In this case you are listing everything in the same line instead of breaking.

inserir a descrição da imagem aqui

 public void GerarLogoProcessamentoFim()
    {
        StreamWriter objWR = new StreamWriter(processo, true);
        StringBuilder linha = new StringBuilder();
        linha.Append("      --> NF PROCESSADAS: " + processadas);
        linha.Append("      --> NF NÃO PROCESSADAS: " + nprocessadas);
        if (nprocessadasNumero.Count > 0)
        {
            for (int i = 0; i < nprocessadasNumero.Count; i++)
            {
                linha.Append("NF - " + nprocessadasNumero[i]);
            }
        }
        linha.Append("      <-- FINALIZAÇÃO:" + DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
        objWR.WriteLine(linha.ToString());
        objWR.Close();
    }
  • 1

    only use character for new line: \n example: linha.Append("NF - " + nprocessadasNumero[i] + "\n");

4 answers

1


Why don’t you use

objWR.Write(string)

and

objWR.WriteLine(string)

instead of mounting a line on a Stringbuilder and printing, at the end of the loop, inside objWR? At the end of the day, it’s objWR that you want out, so I used methods of that.

1

Write, write without skipping line and Writeline, write line and jump

objWR.Writeline(string)

Or

These two below are equivalent, if you debug you will see that this Environment.Newline is "/r/n":

line. Append(Environment.Newline); OR line. Append("/r/n");

0

You can use:

linha.AppendLine();

0

linha.Append(Environment.NewLine);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.