views:

106

answers:

5

What am I doing wrong in the following code?

public string ReadFromFile(string text)
    {
        string toReturn = "";
        System.IO.FileStream stream = new System.IO.FileStream(text, System.IO.FileMode.Open);
        System.IO.StreamReader reader = new System.IO.StreamReader(text);
        toReturn = reader.ReadToEnd();
        stream.Close();
        return toReturn;
    }

I put a text.txt file inside my bin\Debug folder and for some reason, each time when I enter this file name ("text.txt") I am getting an exception of System.IO.FileNotFoundException.

+3  A: 

The issue of not finding the file... that depends on a number of things. The file should be found relative to the current working directory. I'd imagine your .NET version may make a difference to this. Whether you're running the Debug or Release version will make a difference to where the file is looked for. And finally whether this is a web application or a desktop application. Web applications (Visual Studio 2005) run from a temporary cache so you can never get a text file in there without building it into your project beforehand (in the Solution Explorer) or by generating it as part of the execution.

To discover where your executing code thinks it is, try:

Console.WriteLine(Directory.GetCurrentDirectory());

Alright, so you weren't using your stream, but that was a minor point. I've updated the parameter name... you know since it makes more sense, and finally included the current directory in an error message so you can see it's not found and where it was looking.

public string ReadFromFile(string filename)
    {
        string toReturn = "";
        if (!File.Exists(filename))
            return "File (" + filename+ ") not found (in "+Directory.GetCurrentDirectory()+")";
        System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
        System.IO.StreamReader reader = new System.IO.StreamReader(stream);
        toReturn = reader.ReadToEnd();
        stream.Close();
        return toReturn;
    }
Rudu
that was indeed a mistake but still even after changing as you said i should i still got the same exception when i am debugging i can see that stream got null inside him when the program crush
Nadav Stern
Fair enough @Nadav, added an error message to explain where the code's looking (to help you figure out why you're not finding the file).
Rudu
+4  A: 

It is not safe to assume that the current working directory is identical to the directory in which your binary is residing. You can usually use code like the following to refer to the directory of your application:

string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string filename = System.IO.Path.Combine(applicationDirectory, text);

This may or may not be a solution for your given problem. On a sidenote, text is not really a decent variable name for a filename.

Jim Brissom
A: 

File.ReadAllText(path) does the same thing as your code. I would suggest using rooted path like "c:......\text.txt" instead of the relative path. The current directory is not necessarily set to your app's home directory.

Serge
A: 

You can use Process Monitor (successor to FileMon) to find out exactly what file your application tries to read.

Albin Sunnanbo
A: 

My suggestions:

public string ReadFromFile(string fileName)
        {
            using(System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
            using(System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            {
               return = reader.ReadToEnd();
            }
        }

or even

string text = File.OpenText(fileName).ReadToEnd();

You can also check is file exists:

if(File.Exists(fileName)) 
{
   // do something...
}

At last - maybe your text.txt file is open by other process and it can't be read at this moment.

UGEEN