Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
This piece of code directly pasted on cs file,

C#
using System;
using System.IO;

namespace csharp_station.howto
{
    class TextFileReader
    {
        static void Main(string[] args)
        {
            // create reader & open file
            TextReader tr = new StreamReader("date.txt");

            // read a line of text
            Console.WriteLine(tr.ReadLine());

            // close the stream
            tr.Close();
        }
    }
}



give me one of those errors 'type or reference textreader not found, missing directive or reference'.

I noticed that in solution explorer, System.IO is not present on References. how could I add it? Yes, I'm a noob working with VS...

What could be the problem?
Posted
Updated 23-May-11 6:12am
v2

Replace

C#
Textreader tr = new StreamReader("date.txt");


with


C#
StreamReader tr = new StreamReader("date.txt");


By the way, there no situations when hard-code file names should be used, neither absolute no relative paths. Do you know that this code depends on the working directory which depends on how the user runs your application?
All paths should be calculated during run time based on your entry assembly location of some configuration file data. Alternatively, you get file paths form the user via file dialogs.

—SA
 
Share this answer
 
v2
Comments
Maxdd 7 23-May-11 11:36am    
It was just for testing :) Thanks.
Sergey Alexandrovich Kryukov 23-May-11 12:07pm    
Good that you understand it. :-)
Thanks for accepting this answer.
Good luck,
--SA
ambarishtv 23-May-11 11:42am    
both of them are working!!! please see my solution
Sergey Alexandrovich Kryukov 23-May-11 12:06pm    
Your solution is essentially the same. Yes, of course, it should work, too.
--SA
Maxdd 7 23-May-11 12:13pm    
Yes it's true, thank you both.
Its working ,try this code :)
C#
try
{
    string line = null;
    System.IO.TextReader readFile = new StreamReader("C:\\date.txt");
    line = readFile.ReadToEnd();
    Console.WriteLine(line);
    Console.ReadLine();
    readFile.Close();
    readFile = null;
}
catch (IOException ex)
{
    Console.WriteLine(ex.Message);
    Console.ReadLine();
}
 
Share this answer
 
v4
Comments
Maxdd 7 23-May-11 11:36am    
I already tried that :)
ambarishtv 23-May-11 11:56am    
Replace Textreader with TextReader
Sergey Alexandrovich Kryukov 23-May-11 12:07pm    
Sure, you spotted this as I did, probably at the same time, a 5.
--SA
ambarishtv 23-May-11 12:09pm    
Thank you
Maxdd 7 23-May-11 12:12pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900