Tuesday, 19 June 2012

How to write to a Text file or a notepad file or a .txt file or AppendText() method in .NET

Hi friends, in this article I will explained about how to write to a Text file or a notepad file or a  .txt file or AppendText() method in .NET.
                      I already explained in the previous article How to Create a Text File or notepad file using C# and VB.NET, in that i was using CreateText() method. If you want to write the data to a already file then we are using the AppendText() method.
Let I explained through the example.In the below example i am using the StreamWriter class.StreamWriter is class ,is used for write the data to a file.It founds in System.IO namespace.
VB.NET Code:
Imports System.IO
Imports System.Text
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim swriter As StreamWriter
        swriter = File.AppendText("c:\temp\filename.txt")
        swriter.WriteLine("Hi welcome to Aspdotnet-Kishore.blogspot.in")
        swriter.Close()

    End Sub
End Class

C# Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
Public Class Form2
{
     private void Form2_Load(System.Object sender, System.EventArgs e)
     {
          StreamWriter swriter = null;
          swriter = File.AppendText("c:\\temp\\filename.txt");
          swriter.WriteLine("Hi welcome to Aspdotnet-KIshore.blogspot.in");
          swriter.Close();
     }
    Public Form2()
     {
          Load += Form2_Load;
     }
}
                      When we execute the above code then it will append the Hi welcome to Aspdotnet-Kishore.blogspot.in to the filename.txt file.If filename.txt file is not located in the c:\temp then it will craeted the filename.txt file automatically.

"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.