Load txt file through a list c#

Asked

Viewed 551 times

3

I make a query in the bank and put it in a list, then see each item of the list through the foreach to view other tables according to the Id of each row traveled. If found, I would like to upload a file txt for each consulted line, bouncing inside the file txt.

EX:

  • maria street 3 city Sao paulo
  • Sidnei street 3 city Campinas

What I can’t do is delimit the total length of the line because it has to be fixed. I’m doing it in C# with StringBuilder: Ex:

 StringBuilder sb = new StringBuilder(); 
    foreach (var item in registros) 
    { 
        string testeID = Convert.ToString(item.Cells["ID"].Value.ToString());         
        string testecodigo = Convert.ToString(item.Cells["CODIGO"].Value.ToString());
    }


  StringBuilder sb = new StringBuilder();
            foreach (var item in registros)
            {
                 areaNaoConstaRecebe = " ";
                folhaRecebe = Convert.ToString(item.Cells["FOLHA"].Value.ToString());
                folhaRecebe = folhaRecebe.PadRight(5, ' ');
                sb.AppendFormat("{0}{1}{2}",
                 idRegistro = "1",
                 numeroControle = "          ",
                 folhaRecebe ="uuuuu",

The first query saves a row from the list, the second query can have 5 lines and should be populated. Then the third for having 2 line and should be recorded, then it colta the main list and makes again the query and so on. The size would be 832 columns.

  • Show me what you’ve done. And tell me where you’re having a hard time.

  • this is the command I use to save. .Environment.Newline); "File.Writealltext(@"c: Clients_formatted.txt", Sb.Tostring())"

  • Do you mean q each time q vc makes a query it wipes the file and starts again? If not, what would be the problem q is having, I didn’t get it right.

  • The problem is that when recording the first line, the second line is also saved on the same line. A sequence is recorded with the data of the first and the second,

1 answer

1

Add a AppendLine in the end

sb.AppendFormat("{0}{1}{2}",
                     idRegistro = "1",
                     numeroControle = "          ",
                     folhaRecebe ="uuuuu")
  .AppendLine();
  • Good Bruno... gave very right. Thank you very much for the tip....

  • 1

    @Joeliasandrade Thank by voting and accepting the answer.

Browser other questions tagged

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