reading contents of text file into string

  • Thread starter Thread starter Mike P
  • Start date Start date
Mike,

There's a few ways - here's just one of them:

TextReader oTR = null;

// Open as text File
oTR = File.OpenText(<your filename goes here>);

// Local vars
string sBuffer = null;

// Read and Load values
while ((sBuffer = oTR.ReadLine()) != null)
{ // data will be in sBuffer

}
oTR.Close();
 
Probably easiest and safest is:

string s = "";
using(StreamReader rdr = File.OpenText(filename))
{
s = rdr.ReadToEnd();
}

HTH
Kieran
 
Why do I get the error that the file cannot be found in
C:\WINNT\SYSTEM32? I am writing a web application in ASP.NET and I
thought that the default path was that of my web app (i.e.
C:\INETPUB\WWWROOT etc?


Thanks,

Mike
 
Mike,
Mayble that is where the ASP.NET hook into IIS dll is? Sorry I'm not
particulary knowledgeable about ASP.NET. Try looking at
Application.ExecutablePath? I think there is a static member on one of the
IO classes called GetCurrentDirectory or something, although I can't check
at the moment as I'm at work and quite busy! Have a search in the MSDN for
it.

HTH
Kieran
 
Back
Top