P
Phil
Problem code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Streams {
class Program {
static void Main(string[] args) {
const string file = @"C:\Temp\file.txt";
if (File.Exists(file))
File.Delete(file);
using (StreamWriter sw = new StreamWriter(file)) {
sw.WriteLine("Line 1");
sw.WriteLine("Line 2");
sw.WriteLine("Line 3");
FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(fs != null);
fs.Seek(0, SeekOrigin.Begin);
sw.WriteLine("Line A");
}
}
}
}
Expected result: a file with 3 lines
Line A
Line 2
Line 3
Actual result: a file with 4 lines
Line 1
Line 2
Line 3
Line A
The Seek() is being ignored.
Obviously I am doing something wrong - any hints guys?!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Streams {
class Program {
static void Main(string[] args) {
const string file = @"C:\Temp\file.txt";
if (File.Exists(file))
File.Delete(file);
using (StreamWriter sw = new StreamWriter(file)) {
sw.WriteLine("Line 1");
sw.WriteLine("Line 2");
sw.WriteLine("Line 3");
FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(fs != null);
fs.Seek(0, SeekOrigin.Begin);
sw.WriteLine("Line A");
}
}
}
}
Expected result: a file with 3 lines
Line A
Line 2
Line 3
Actual result: a file with 4 lines
Line 1
Line 2
Line 3
Line A
The Seek() is being ignored.
Obviously I am doing something wrong - any hints guys?!