Showing posts with label VB. Show all posts

Write Data to file in .Net ( VB and C# )



This tutorial will go over how to write data to a text file in VB.Net and C#.Net

.Net uses StreamWriter Class To write data to file. StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

VB .Net

Dim objWriter As New System.IO.StreamWriter("d:\abc.txt", True)  
objWriter.WriteLine("Some Text here")
objWriter.Close()

C# .Net

using (System.IO.StreamWriter objWriter = new System.IO.StreamWriter(@"abc.txt", true))
{
       objWriter.WriteLine("Some Text here");
}
objWriter.Close(); 
 

Read Data from text file in .Net ( VB and C#)



This tutorial will go over how to read data from a text file in VB.Net and C#.Net

.Net uses StreamReader Class To read data. StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.

VB .Net

Dim line As String = ""
Dim objReader As New System.IO.StreamReader("d:\abc.txt", True) 
Do line = objReader.ReadLine()
Console.WriteLine(line)
Loop Until line Is Nothing
objReader.Close()

C# .Net
using (StreamReader objReader = new StreamReader("abc.txt")) 
{
    string line;
    // Read and display lines from the file
    while ((line = objReader.ReadLine()) != null) 
    {
         Console.WriteLine(line);
    }
}