joe_pool_is Posted May 21, 2008 Posted May 21, 2008 I used to append a file using Classic ASP on my website. I finally migrated parts over to .NET, but I can't seem to find out how to append a file. I can still determine if the file exists using File.Exists, but I don't know how to fill it! if (File.Exists(strPath) == true) { FileStream fs = null; try { fs = File.Open(strPath, FileMode.Append); char[] data = historyMsg.ToCharArray(); byte[] bdat = new byte[2 * data.Length]; for (int i = 0; i < (2 * data.Length); i++) { // how do I fill this part??? } fs.Write(bdat, 0, bdat.Length); } catch (Exception err) { Response.Write("<b>Unable to append to log file: <font color=\"red\">" + err.Message + "</font></b><br/><br/>"); ok = false; } finally { if (fs != null) { fs.Close(); } } Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted May 21, 2008 Administrators Posted May 21, 2008 Once you have opened the file stream you could either use it's Write method or a possibly even easier way would be to open a StreamWriter over the FileStream and use it to write the string(s) directly. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted May 21, 2008 Author Posted May 21, 2008 I had to look up how to use StreamWriter, but you're right! It was MUCH easier! My code is now simply:if (File.Exists(strPath) == true) { using (StreamWriter sw = File.AppendText(strPath)) { sw.Write(historyMsg); sw.WriteLine("<hr>"); } }Thank you! Again, I owe you a cup of coffee! Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.