jeremychan
jeremychan

Reputation: 4459

appending textfile using streamwriter

I have this code, where i would run the following code in sequence.

I will always create a new text file at the beginning. then for the 2nd and 3rd portion of the code i just need to append the text file "checksnapshot" how do i do that using streamwriter?

//1
using (StreamReader sr = new StreamReader("C:\\Work\\labtoolssnapshot.txt")) ;
{
    string contents = sr.ReadToEnd();
    using (StreamWriter sw = new StreamWriter("C:\\Work\\checksnapshot.properties"))
    {
        if (contents.Contains(args[0]))
        {
            sw.WriteLine("SASE= 1");
        }
        else
        {
            sw.WriteLine("SASE= 0");
        }
    }
}

//2
using (StreamReader sr = new StreamReader("C:\\Work\\analyzercommonsnapshot.txt")) ;
{
    string contents = sr.ReadToEnd();
    using (StreamWriter sw = new StreamWriter("C:\\Work\\checksnapshot.properties"))
    {
        if (contents.Contains(args[0]))
        {
            sw.WriteLine("Analyzer= 1");
        }
        else
        {
            sw.WriteLine("Analyzer= 0");
        }
    }
}

//3
using (StreamReader sr = new StreamReader("C:\\Work\\mobilesnapshot.txt")) ;
{
    string contents = sr.ReadToEnd();
    using (StreamWriter sw = new StreamWriter("C:\\Work\\checksnapshot.properties"))
    {
        if (contents.Contains(args[0]))
        {
            sw.WriteLine("mobile= 1");
        }
        else
        {
            sw.WriteLine("mobile= 0");
        }
    }
}

Upvotes: 1

Views: 8116

Answers (4)

Dani
Dani

Reputation: 2550

use FileStream instead of StreamWriter:

using (FileStream fs = new FileStream("C:\\Work\\checksnapshot.properties",FileMode.OpenOrCreate,FileAccess.Append))
{
    StreamWriter writer = new StreamWriter(fs);
    writer.Write(whatever);
}

note: I've only used this in .NET 4

Upvotes: 1

Steve B
Steve B

Reputation: 37660

I don't why your code does not works, but why don't you use the builtin methods :

string contents = File.ReadAllText("C:\\Work\\labtoolssnapshot.txt");
string contents2 = File.ReadAllText("C:\\Work\\analyzercommonsnapshot.txt");
string contents3 = File.ReadAllText("C:\\Work\\mobilesnapshot.txt");
string outFile = "C:\\Work\\checksnapshot.properties";
//1 
if (contents.Contains(args[0]))
{
    File.WriteAllText(outFile,"SASE=1");
}
else
{
    File.WriteAllText(outFile,"SASE=0");
}
//2 
if (contents2.Contains(args[0]))
{
    File.AppendAllText(outFile,"Analyzer= 1");
}
else
{
    File.AppendAllText(outFile,"Analyzer= 0");
}

//3
if (contents3.Contains(args[0]))
{
    File.AppendAllText(outFile,"mobile= 1");
}
else
{
    File.AppendAllText(outFile,"mobile= 0");
}

Or, in an even more laziest code :

var contents = File.ReadAllText("C:\\Work\\labtoolssnapshot.txt");
var contents2 = File.ReadAllText("C:\\Work\\analyzercommonsnapshot.txt");
var contents3 = File.ReadAllText("C:\\Work\\mobilesnapshot.txt");
var outFile = "C:\\Work\\checksnapshot.properties";

File.WriteAllText(outfile, string.Format(
@"SASE= {0}
Analyzer= {1}
mobile= {2}
",
    contents.Contains(args[0]) ? "1" : "0",
    contents2.Contains(args[0]) ? "1" : "0",
    contents3.Contains(args[0]) ? "1" : "0"
    ));

Upvotes: 1

ChrisWue
ChrisWue

Reputation: 19020

By using the proper constructor of StreamWriter:

new StreamWriter(someFile, true)

will open someFile and append.

Upvotes: 4

crypted
crypted

Reputation: 10306

What about doing this,

new StreamWriter("C:\\Work\\checksnapshot.properties",true)

true means append if the file Exists.

Upvotes: 9

Related Questions