Connecting Tech Pros Worldwide Help | Site Map

reading contents of text file into string

Mike P
Guest
 
Posts: n/a
#1: Nov 15 '05
How do I read the contents of a text file into a string in my program?


Thanks,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
PeteZ
Guest
 
Posts: n/a
#2: Nov 15 '05

re: reading contents of text file into string


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();


"Mike P" <mrp@telcoelectronics.co.uk> wrote in message
news:eDpnGoDeDHA.2228@TK2MSFTNGP12.phx.gbl...[color=blue]
> How do I read the contents of a text file into a string in my program?
>
>
> Thanks,
>
> Mike
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]


Kieran Benton
Guest
 
Posts: n/a
#3: Nov 15 '05

re: reading contents of text file into string


Probably easiest and safest is:

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

HTH
Kieran

"Mike P" <mrp@telcoelectronics.co.uk> wrote in message
news:eDpnGoDeDHA.2228@TK2MSFTNGP12.phx.gbl...[color=blue]
> How do I read the contents of a text file into a string in my program?
>
>
> Thanks,
>
> Mike
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]


Mike P
Guest
 
Posts: n/a
#4: Nov 15 '05

re: reading contents of text file into string


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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Kieran Benton
Guest
 
Posts: n/a
#5: Nov 15 '05

re: reading contents of text file into string


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


"Mike P" <mrp@telcoelectronics.co.uk> wrote in message
news:OcFvxTEeDHA.3820@tk2msftngp13.phx.gbl...[color=blue]
> 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
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]


Martin Bischoff
Guest
 
Posts: n/a
#6: Nov 15 '05

re: reading contents of text file into string


Use Server.MapPath(<your filename>) in ASP.NET to get the correct path.

---
Martin Bischoff

Mike P wrote:[color=blue]
> 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?[/color]

Closed Thread