Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Wednesday, March 19, 2008

Sample 1 demonstrates how to use the StreamReader technique when reading the contents of a file

[C# Version]

using System;
using System.IO;

namespace KeithRull.ReadMyFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //the filename of the file to read
            string xmlFilename = "Symbols.xml";
          
            //validate if the file exist
            if (File.Exists(xmlFilename))
            {
                //create a new StreamReader object tha would read the file
                using (StreamReader sReader = new StreamReader(xmlFilename))
                {
                    //create a string object that would hold the
                    //value of each line in our file
                    string line = String.Empty;
                    
                    //iterate for each line item in our file
                    while ((line = sReader.ReadLine()) != null)
                    {
                        //display the output in the screen
                        Console.WriteLine(line);
                    }
                }
            }
            else
            {
                //notify that the file was not found
                Console.WriteLine("File not found!");
            }

            //pause and wait for the user.
            Console.ReadLine();
        }
    }
}

[VB.NET Version]

Imports System 
Imports System.IO 

Namespace KeithRull.ReadMyFile 
    Class Program 
        Private Shared Sub Main(ByVal args As String()) 
            'the filename of the file to read 
            Dim xmlFilename As String = "Symbols.xml" 
            
            'validate if the file exist 
            If File.Exists(xmlFilename) Then 
                'create a new StreamReader object tha would read the file 
                Using sReader As New StreamReader(xmlFilename) 
                    'create a string object that would hold the 
                    'value of each line in our file 
                    Dim line As String = [String].Empty 
                    
                    'iterate for each line item in our file 
                    While (line = sReader.ReadLine()) IsNot Nothing 
                        'display the output in the screen 
                        Console.WriteLine(line) 
                    End While 
                End Using 
            Else 
                'notify that the file was not found 
                Console.WriteLine("File not found!") 
            End If 
            
            'pause and wait for the user. 
            Console.ReadLine() 
        End Sub 
    End Class 
End Namespace 
Wednesday, March 19, 2008 6:52:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
Comments are closed.
On this page
Archive
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 246
This Year: 43
This Month: 4
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)