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;
}