C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to read all the files in a specified directory


Posted Date: 20 May 2008    Resource Type: Code Snippets    Category: File Operations
Author: LakshmiMember Level: Gold    
Rating: Points: 10



The following code sample shows how to read all the files in a specified directory.


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Generics
{
public class ReadDirectory
{
#region Member Variable

//Directory path given as string.
private string directoryPath = @"C:\Session\Help Links";

#endregion

#region Constructor

public ReadDirectory()
{
//Below lines to find out whether specified directory
//exists or not.
DirectoryInfo readDirectory = new DirectoryInfo(directoryPath);

if (!readDirectory.Exists)
{
Console.WriteLine("Directory not found");

return;
}

//Below line used to read the total files available inside
//the specified directory.
FileInfo[] filesInDirectory = readDirectory.GetFiles();

//Calling a method by passing name parameter,which
//is of type FileInfo[].
ReadFiles(filesInDirectory);
}

#endregion

#region Method - File Reading

///
/// Represents the method that reads the full file.
///

///
/// The represents the files available in the
/// specified directory.
///
public void ReadFiles(FileInfo[] fileInDirectory)
{
//Foreach used to loop thro the each files
//present in the directory.
foreach (FileInfo files in fileInDirectory)
{
FileInfo readfile = new FileInfo(files.FullName);

if (!readfile.Exists)
return;

string[] fileLines = File.ReadAllLines(readfile.ToString());

Console.WriteLine("Reading the file " + readfile + "....\n");

//Loop used to read the characters inside a particular file.
for (int i = 0; i < fileLines.Length; i++)
{
Console.WriteLine(fileLines[i]);

if (i == fileLines.Length - 1)
{
Console.WriteLine("End of file" + readfile + ".\n");
}
}
Console.ReadLine();
}
}

#endregion
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Reading files  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to read a text file and store into a variable
Previous Resource: How to Get File in a specific folder using asp.net and C#
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use