ARTICLE

Read a file using C#

Posted by Pramod Singh Articles | C# Language January 15, 2000
This program just demonstrate the use of FileStream & StreamReader.
Reader Level:
Download Files:
 

This program just demonstrate the use of FileStream & StreamReader. The program take 1 parameter from the user i.e. the file to read. 

using System;
using System.IO;
class FileRead
{
string filereadbuf; // buffer to store the content of file
public void ReadFile(string FileName, int FileSize)
{
char[] buf = new char[FileSize]; // lets define an array of type char field (i.e. variable) buf
// for more help please see .net sdk
StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
int retval = sr.ReadBlock(buf, 0, FileSize); // no. of bytes read
Console.Write ("Total Bytes Read = " + retval + "\n");
filereadbuf =
new string(buf); // store it in our field
Console.WriteLine (filereadbuf); // lets print on screen
sr.Close();
}
}
class TestFileRead
{
public static void Main(string[] args)
{
String[] cmdline = Environment.GetCommandLineArgs();
// Get the command line parameter
Console.WriteLine("File Reader Using Stream Reader & File Stream \n");
if (cmdline.Length < 2) // If no parameter is given will show user the usage
{
Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
return;
}
// Using Directory Class & using a Method GetFiles we get file list from the current directory
// return value is array of files please see .net sdk documentation for more help.
File[] fe = (
new Directory(".")).GetFiles(cmdline[1]);
if (fe.Length == 0)
{
Console.WriteLine(cmdline[1] + ": file not found");
// if not found display a message to user
return;
}
FileRead fr =
new FileRead();
try
{
fr.ReadFile(cmdline[1], (
int)fe[0].Length); // sends 2 parameter filename & length
}
catch(IOException e)
{
Console.WriteLine("I/O error occured" + e);
return ;
}
}
// Close brace of Main
} // close brace of TestFileRead 

I would like to give  thanks to Saurabh for his experts help, Mahesh for loading my code on his site (it rocks) and thanks to you too for reading it you know better who you are.

If you have any comments or doubts or find something terrible wrong in program or comments. Please feel free to e-mail me !

Login to add your contents and source code to this article
post comment
     

4 error comes !!! doesn't work !!!

Posted by Rahat Mar 04, 2011

when I read a text file that is bigger than 4 MB, the web page is crash

Posted by Kevin Zhang Mar 10, 2008

hi Pramod im begginer in c# i write a web code in c#.net for uploading files. but i dont now what should i do that the user can see and download the file. woukd you please help me. would you please send your replay to ebrahim_yazdi1@yahoo.com becouse i dont now how i can see your replay in this file. thanx for your article

Posted by ebrahim yazdi Sep 11, 2007
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts