C# Keep file open but overwrite contents?

Use File. Open with FileMode Truncate to create the file stream for your TextWriter.

Use the text writer, but clear the contents of the file before you begin writing. Something like this: string path = null;//path of file byte bytes_to_write = null; System.IO.File. WriteAllText(path, string.

Empty); System.IO. FileStream str = new System.IO. FileStream(path, System.IO.FileMode.

Open, System.IO.FileAccess. Write, System.IO.FileShare. Read); str.

Write(bytes_to_write, 0, bytes_to_write. Length); Perhaps something from this example will help?

Clearing the file each time in my loop using System.IO.File. WriteAllText(path, string. Empty); will also lead to the same overhead since I will have to do it repeatedly in a loop.

– newidforu Apr 13 at 18:39.

Pass false as the append parameter of the constructor: TextWriter tsw = new StreamWriter(path, false); Ref: msdn.microsoft.com/en-us/library/system.....

Have you tried using Thread. Sleep? msdn.microsoft.com/en-us/library/system.....

Here is how I did it in Silverlight 4. Since you are not using Silverlight, you won't use isolated storage, but same technique will work regardless of the backing store. The interesting bit is in the Write() method: logWriter.BaseStream.

SetLength(0); From Stream. SetLength Method: When overridden in a derived class, sets the length of the current stream.Be sure to flush the stream using either AutoFlush (as I did in this example), or by adding a logWriter.Flush() after the logWriter.Write(). /// /// Represents a log file in isolated storage.

/// public static class Log { private const string FileName = "TestLog. Xml"; private static IsolatedStorageFile isoStore; private static IsolatedStorageFileStream logWriterFileStream; private static StreamWriter logWriter; public static XDocument Xml { get; private set; } static Log() { isoStore = IsolatedStorageFile. GetUserStoreForApplication(); logWriterFileStream = isoStore.

OpenFile( FileName, FileMode. Create, FileAccess. Write, FileShare.

None); logWriter = new StreamWriter(logWriterFileStream); logWriter. AutoFlush = true; Xml = new XDocument(new XElement("Tests")); } /// /// Writes a snapshot of the test log XML to isolated storage. /// public static void Write(XElement testContextElement) { Xml.Root.

Add(testContextElement); logWriter.BaseStream. SetLength(0); logWriter. Write(Xml.ToString()); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions