P: n/a | Have a look at the following code from MSDN library. Cheers, Denis using System; using System.IO; class Test { public static void Main() { string path = @'c:tempMyTest.txt'; // This text is added only once to the file. if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine('Hello'); sw.WriteLine('And'); sw.WriteLine('Welcome'); } } // This text is always added, making the file longer over time // if it is not deleted. using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine('This'); sw.WriteLine('is Extra'); sw.WriteLine('Text'); } // Open the file to read from. using (StreamReader sr = File.OpenText(path)) { string s = '; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } } 'Roy Gourgi' <ro***@videotron.ca> wrote in message news:Kn*******************@wagner.videotron.net... Hi, How could I append a text file to another text file. Let's say that I havea File1 and I want to append File2 at the end of File1, how would I do that? TIA Roy
|
|
You can simply call. Using (StreamWriter w = File.AppendText('log.txt')) It will create the file if it doesn't exist and open the file for appending. Writing data into a text file using streamwriter in c#. Archived Forums V. If i use 'FileMode.Append', all the data will be appended to the file. On screen this looks right, yet it overwrites the file every time? Public static bool SaveTextToFile(string strData, string FullPath) { bool bAns = false. If the file does exist, write operations to the StreamWriter append text to the file. Additional threads are permitted to read the file while it is open. The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Create File If File Does Not.
C Streamwriter Append Text File C#
P: n/a | At the moment its just a simple string strErrorMessage = dCurrentTime.Date + ' ' + dCurrentTime.TimeOfDay + ': ' + strFile + ', Line ' + nLine.ToString() + ': ' + strError; strFile and strError are also strings with test text such as 'Test' The file with the pre-existing text that I am trying to append, was created and previously written to in C++. Is this maybe an encoding problem? If so, how would I go about fixing it? Thanks for helping 'Jon Skeet [C# MVP]' wrote: Duckwon <Du*****@discussions.microsoft.comwrote: My code is suppose to write to an existing file. But after my C# code appends the file, the previous text loses all the endline characters, becoming one long line. How can I retain the formating of the pre-existing text? FileStream stream = new FileStream(strFileName, FileMode.Append, FileAccess.Write); StreamWriter writer = new StreamWriter(stream); writer.WriteLine(strErrorMessage); Thanks in advancefor any help.... You haven't shown how you're constructing strErrorMessage in the first place. Could you post a short but complete program which demonstrates the problem? See http://www.pobox.com/~skeet/csharp/complete.html for details of what I mean by that. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
|
|