Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how do i delete lines from .txt file? "Urgent!"

how do i delete lines from .txt file? "Urgent!"

Scheduled Pinned Locked Moved C#
questioncareer
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andredani
    wrote on last edited by
    #1

    How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:

    C P 2 Replies Last reply
    0
    • A andredani

      How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:

      C Offline
      C Offline
      Christian Wulff
      wrote on last edited by
      #2

      Hi, you can do that if you create a temporary file, copy only the lines which should not be deleted to that temp file and later overwrite the original file with the temp file. E.g:

      string tempFilename = System.IO.Path.GetTempFileName();
      using (StreamReader sr = new StreamReader(path1 + ""))
      using (StreamWriter sw = new StreamWriter(tempFilename))
      {
      while (sr.Peek() > 0)
      {
      line = sr.ReadLine();
      if (line == "#")
      {
      name = sr.ReadLine();
      nameAndJob = string.Concat(name);
      Job = string.Concat(name);
      if (list.ContainsKey(nameAndJob))
      {
      item1 = list[nameAndJob];
      item1 = list[Job];
      item1.SubItems[1].Text = sr.ReadLine();
      item1.SubItems[3].Text = sr.ReadLine();
      item1.SubItems[5].Text = sr.ReadLine();
      /////////// Can i delete this lines in .txt file
      }
      else
      {
      // Write the "#" line
      sw.WriteLine("#");
      // Write the name which was read above
      sw.WriteLine(name);
      // The next three lines will be written in the else block below
      }
      }
      else
      {
      sw.WriteLine(line);
      }
      }
      sw.Flush();
      }
      System.IO.File.Delete(path1 + "/personalpr.txt");
      System.IO.File.Move(tempFilename, path1 + "/personalpr.txt");

      I didn't test the code, but I think you want something like that.

      A 1 Reply Last reply
      0
      • A andredani

        How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #3

        You asked the same question a couple of hours ago and I answered it. Please don't double-post your questions - it's rude.:( Paul

        1 Reply Last reply
        0
        • C Christian Wulff

          Hi, you can do that if you create a temporary file, copy only the lines which should not be deleted to that temp file and later overwrite the original file with the temp file. E.g:

          string tempFilename = System.IO.Path.GetTempFileName();
          using (StreamReader sr = new StreamReader(path1 + ""))
          using (StreamWriter sw = new StreamWriter(tempFilename))
          {
          while (sr.Peek() > 0)
          {
          line = sr.ReadLine();
          if (line == "#")
          {
          name = sr.ReadLine();
          nameAndJob = string.Concat(name);
          Job = string.Concat(name);
          if (list.ContainsKey(nameAndJob))
          {
          item1 = list[nameAndJob];
          item1 = list[Job];
          item1.SubItems[1].Text = sr.ReadLine();
          item1.SubItems[3].Text = sr.ReadLine();
          item1.SubItems[5].Text = sr.ReadLine();
          /////////// Can i delete this lines in .txt file
          }
          else
          {
          // Write the "#" line
          sw.WriteLine("#");
          // Write the name which was read above
          sw.WriteLine(name);
          // The next three lines will be written in the else block below
          }
          }
          else
          {
          sw.WriteLine(line);
          }
          }
          sw.Flush();
          }
          System.IO.File.Delete(path1 + "/personalpr.txt");
          System.IO.File.Move(tempFilename, path1 + "/personalpr.txt");

          I didn't test the code, but I think you want something like that.

          A Offline
          A Offline
          andredani
          wrote on last edited by
          #4

          it worked just fine, you are the best man!! now it only deletes the lines hwo gets in use, right? have another question for you because you are so good....:) can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1. and the item are saved in .txt file like this # 1 2 3 4 # 5 6 7 8 :doh:

          C 1 Reply Last reply
          0
          • A andredani

            it worked just fine, you are the best man!! now it only deletes the lines hwo gets in use, right? have another question for you because you are so good....:) can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1. and the item are saved in .txt file like this # 1 2 3 4 # 5 6 7 8 :doh:

            C Offline
            C Offline
            Christian Wulff
            wrote on last edited by
            #5

            andredani wrote:

            now it only deletes the lines hwo gets in use, right?

            Yes, it should only write the lines which are not used in the temp file (and therewith deleting the used lines).

            andredani wrote:

            can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1.

            I'm not quite sure that i understand what you mean. Let's assume your file looks like (I added line numbers): # (line 1) 5 (line 2) 6 (line 3) 5 (line 4) 6 (line 5) Normally your SubItems would be filled like this: SubItems[0] = 5 SubItems[1] = 6 SubItems[3] = 5 SubItems[5] = 6 But because line 2 = line 4 and line 3 = line 5 you want to add 1 to SubItems[1], so that the SubItems are: SubItems[0] = 5 SubItems[1] = 7 (6 from line 3 + 1) SubItems[3] = 5 SubItems[5] = 6 Is that what you mean?

            A 1 Reply Last reply
            0
            • C Christian Wulff

              andredani wrote:

              now it only deletes the lines hwo gets in use, right?

              Yes, it should only write the lines which are not used in the temp file (and therewith deleting the used lines).

              andredani wrote:

              can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1.

              I'm not quite sure that i understand what you mean. Let's assume your file looks like (I added line numbers): # (line 1) 5 (line 2) 6 (line 3) 5 (line 4) 6 (line 5) Normally your SubItems would be filled like this: SubItems[0] = 5 SubItems[1] = 6 SubItems[3] = 5 SubItems[5] = 6 But because line 2 = line 4 and line 3 = line 5 you want to add 1 to SubItems[1], so that the SubItems are: SubItems[0] = 5 SubItems[1] = 7 (6 from line 3 + 1) SubItems[3] = 5 SubItems[5] = 6 Is that what you mean?

              A Offline
              A Offline
              andredani
              wrote on last edited by
              #6

              ehh.. now i don´t understand you hehe...:)' i´m gonna try to excplain in easy terms... subitems[0] and subitem[1] have to be unique. .txt fil for example: # Online K411 2007-05-05 20:15:12 AD # Historik K411 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD Here is everything ok because line1 and line2 after# is unique. But this is wrong: # Online K412 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD In this example i have to add +1 on k412 to k413. so its unique. And also check k413 if its unique.. Little hard to excplain.. :-D Need this so bad...:^) Thanks man!

              C 1 Reply Last reply
              0
              • A andredani

                ehh.. now i don´t understand you hehe...:)' i´m gonna try to excplain in easy terms... subitems[0] and subitem[1] have to be unique. .txt fil for example: # Online K411 2007-05-05 20:15:12 AD # Historik K411 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD Here is everything ok because line1 and line2 after# is unique. But this is wrong: # Online K412 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD In this example i have to add +1 on k412 to k413. so its unique. And also check k413 if its unique.. Little hard to excplain.. :-D Need this so bad...:^) Thanks man!

                C Offline
                C Offline
                Christian Wulff
                wrote on last edited by
                #7

                Does the second line (K412 and K413 in your example) always start with one letter and is followed only by digits? Will there never be Km413 or just 413?

                A 1 Reply Last reply
                0
                • C Christian Wulff

                  Does the second line (K412 and K413 in your example) always start with one letter and is followed only by digits? Will there never be Km413 or just 413?

                  A Offline
                  A Offline
                  andredani
                  wrote on last edited by
                  #8

                  yes always ONE letters and fallowed by 6 numbers... it makes all little bit harder... thanks!!! BUT the last number, is a string hwo starts with 1 like this: string ett = "1"; for exampel: K44322+(ett) = K443221 So "ett" is the only one hwo going to change! Thanks man i´m stuck now need your help!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups