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 3 is similar to Sample 1 and and Sample 2 but it uses the File.ReadAllText method which reads the content of the whole file instead of reading the data line by line or reading the data and putting the values into a string[].

[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))
            {
                //read the contents of the file
                string fileContent = File.ReadAllText(xmlFilename);

                //display the output in the screen
                Console.WriteLine(fileContent);
            }
            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 
                'read the contents of the file 
                Dim fileContent As String = File.ReadAllText(xmlFilename) 
                
                'display the output in the screen 
                Console.WriteLine(fileContent) 
            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 7:18:14 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
Archive
<March 2008>
SunMonTueWedThuFriSat
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345
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: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)