Hey guys,
For some reason, I keep getting a FileNotFound exception while trying to read from a text file, although the path that the StreamReader is looking in is correct - which I know for a fact.
Here is the method that does the reading:
public void setDescription(String file)
{
string currentDir, desc;
try
{
currentDir = Directory.GetCurrentDirectory();
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader(currentDir + "\\" + file);
desc = myFile.ReadToEnd();
myFile.Close();
label11.Text = desc;
}
catch(IOException e)
{
label11.Text = "File read error.\n" + e.Message;
}
}
I have tried this without the current dir and tried reading a file from i.e. C:\file.txt - yet still no success.
Any ideas

Cannot read from a text file - FileNotFound Exception, although file is there
Lovenish
Andreas Öhlund
Have you checked in the debugger that the path is right Because, if it says file not found, then the file plain is not there.
ISV Buddy Team
public void setDescription(String file)
{
string currentDir = Directory.GetCurrentDirectory();
string desc = null;
try
{
string fullname = Path.Combine( currentDir, file );
if( !File.Exists( fullname ) )
{
label11.Text = string.Format( "File '{0}' couldn't be found.", fullname );
}
// Read the file as one string.
using( StreamReader myFile = new System.IO.StreamReader( fullname ) )
{
desc = myFile.ReadToEnd();
myFile.Close();
label11.Text = desc;
}
}
catch(IOException e)
{
label11.Text = "File read error.\n" + e.Message;
}
}