.txt writing help :3

.txt writing help :3

Postby Warren1001 » 23 Jun 2013, 19:17

Code: Select all
using (var write = new StreamWriter())
{
   write.WriteLine();
}


How would you make it remove a line..?
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: .txt writing help :3

Postby Warren1001 » 23 Jun 2013, 21:09

nevermind, found a different way:p
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: .txt writing help :3

Postby ismellike » 24 Jun 2013, 00:25

If you wanted to do it as you specified, do this.

Code: Select all
using(var sr = new StreamReader("path/path.txt"))
using(var sw = new StreamWriter("path/path1.txt"))
{
     string line;
        while((line=sr.ReadLine())!=null)
              {
                   if(!line.Contains("stuff in bad line"))
                {
                     sw.WriteLine(line);
                 }
               }
}
File.Delete("path/path.txt");
File.Move("path/path1.txt","path/path.txt");


So what the code does it reads every line and writes every line it reads. The if statement is used to filter out the line you dont want.
Then when it's done, it deletes the file it was reading and moves the file that it was writing to to the one it was reading.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: .txt writing help :3

Postby joppiesaus » 24 Jun 2013, 07:31

You possibly forgot this:
Code: Select all
using System.IO;
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron