Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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();
         }
       }

Posted

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...