Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9308483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:38:21+00:00 2026-06-19T00:38:21+00:00

I’m reading a file.txt using StreamReader and writing using streamWriter . I’d like to

  • 0

I’m reading a file.txt using StreamReader and writing using streamWriter.

I’d like to know if it’s possible to ´DELETE JUST THE LAST INSERTED LINE?
i'm inserting lines, but sometimes one of these inserts are
string.empty. Then It goes to anexception`that I created… In this exception, I want to delete that line I just inserted.

But I recreate the file, or something like that, I need just to remove/erase/delete the last line. May I do that ?

MyCode: IF you guys have another way to do this, i’d be very thankfull !

using (StreamWriter streamW = new StreamWriter(fileFinal, true))
{
if (contador == numeroCampos)
{
contador = 0;
}

  foreach (string s in clb_frente_impressao.Items) 
    {                            
    if (camposEscritos >= 10 * numeroCampos) 
      {
        streamW.WriteLine();
        streamW.WriteLine("-------------------------------------------------------");
        streamW.Write(nomearquivo + x.ToString());
        streamW.WriteLine();
        x++;
        camposEscritos = 0;
       }

       if (contador >= clb_frente_impressao.Items.Count)
          {
            contador = 0;
          }
       switch (s)
         {
            case "numero_carteira":
            if (campos_obrigatorios.Contains("numero_carteira") && campo.numero_carteira == "")
               {
                 dados_inexistentes++;
                 skipToNext = true;
                 break;
                }
                else if (campo.numero_carteira != "")
                   {
                      string aux = "";
                      qtdZeros = Txt_qtdZeros.Text;
                      if (qtdZeros == "")
                        {
                           qtdZeros = "8";
                        }
                   while (campo.numero_carteira.Length < Convert.ToInt32(qtdZeros))
                       {
                         campo.numero_carteira = campo.numero_carteira.Insert(0, "0");
                       }

                    if (usaC == "sim")
                        {
                      campos += @"\" + "*C" + aux + campo.numero_carteira + "*" + @"\";
                                                camposEscritos++;
                         }
                                            else
                                            {
                                                campos += @"\" + "*" + aux + campo.numero_carteira + "*" + @"\";
                                                camposEscritos++;
                                            }

                                            if (contador == 0)
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-15}", campo.numero_carteira);
                                                contador++;
                                            }
                                            else
                                            {
                                                streamW.Write("{0,-15}", campo.numero_carteira);
                                                contador++;
                                            }
                                        }
                                       break;

                                    case "matricula":
                                       if (campos_obrigatorios.Contains("matricula") && campo.matricula == "")
                                       {
                                           dados_inexistentes++;
                                           skipToNext = true;
                                           break;
                                       }
                                        camposEscritos++;
                                        if (campo.matricula != "")
                                        {
                                            if (campo.tipo_pessoa == "3")
                                            {
                                                campos += @"\" + campo.matricula + "-" + campo.cod_dependente + @"\"; 
                                            }
                                            else
                                            {
                                                campos += @"\" + campo.matricula + @"\"; 
                                            }
                                        }
                                        if (contador > 0)
                                        {
                                            if (campo.cod_dependente != "")
                                            {
                                                streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);                                            
                                                contador++;
                                            }
                                            else
                                            {                                            
                                                streamW.Write("{0,-10}", campo.matricula);
                                                contador++;
                                            }
                                        }
                                        else
                                        {
                                            if (campo.cod_dependente != "")
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);
                                                contador++;
                                            }
                                            else
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-10}", campo.matricula);
                                                contador++;
                                            }
                                        }
                                        break;
  if (skipToNext) break;

                        } //Final do ForEach

                        if (skipToNext)
                        {
                            //HERE IS WHERE I WANT TO DELETE THE LAST LINE THAT WAS WRITED BY streamW
                            continue;
                        }

e.g: When It gets into case:"numero_carteira" and it’s not empty,then it writes ok, but when I get to the matricula AND its empty, it will break and go to the exception I created. I want to delete the line there ;s Hope I could be clear !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:38:23+00:00Added an answer on June 19, 2026 at 12:38 am

    If the file is not too large, you can simply use this code:

    var lines = File.ReadAllLines(pathToFile);
    File.WriteAllLines(pathToFile, lines.Take(lines.Length - 1));
    

    So you have to write all lines except the last.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using an ASP request returning a XML file containing some latin characters. By
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.