By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,885 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,885 IT Pros & Developers. It's quick & easy.

Could not load the file exception when trying to read a file with StreamReader

P: 21

I am using the following code to read a file.

DirectoryInfo dir = new DirectoryInfo(@"C:\");
if (dir.Exists)
FileInfo[] csvfiles = dir.GetFiles("*.csv");
foreach (FileInfo file in csvfiles)
Console.WriteLine(file.Name );
using (StreamReader sr = new StreamReader(file.Name))
string line;
// Read and display lines from the file until the end of
// the file is reached.

while ((line = sr.ReadLine()) != null)


catch (Exception e)

But, this code is running up to streamreader class and giving error saying that could not load file "C:\DotnetApplication\FileOperations\test.csv" - this is the file path where my application exists. The 'test.csv' file actually presented under C:\ directory.
Please advice how to fix this?

Jul 8 '09 #1
Share this Question
Share on Google+
1 Reply

P: 270
When initializing the StreamReader object, you need to pass the full path in the constructor...

Expand|Select|Wrap|Line Numbers
  1. using (StreamReader sr = new StreamReader(dir.FullName+file.Name))
Jul 9 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.