Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to read a text file into a string variable

Reply
Thread Tools

How to read a text file into a string variable

 
 
ad
Guest
Posts: n/a
 
      02-23-2006
I have a text file in the directory of my web application.
How can I read this text file into a string vaiable?


 
Reply With Quote
 
 
 
 
John Timney \( MVP \)
Guest
Posts: n/a
 
      02-23-2006
StreamReader SR;
string S;
SR=File.OpenText(filename);
S=SR.ReadLine();
while(S!=null) {
Console.WriteLine(S);
S=SR.ReadLine();
}
SR.Close();

--
Regards

John Timney
Microsoft MVP

"ad" <> wrote in message
news:...
>I have a text file in the directory of my web application.
> How can I read this text file into a string vaiable?
>



 
Reply With Quote
 
 
 
 
Daniel Fisher\(lennybacon\)
Guest
Posts: n/a
 
      02-23-2006
public string ReadFromFile(string filename)
{
StreamReader sr = File.OpenText(filename);

StringBuilder sb = new StringBuilder();
string str = sr.ReadLine();
while( str != null )
{
sb.Append(str + "\n");
str = sr.ReadLine();
}

sr.Close();
return sb.ToString();
}


Daniel Fisher(lennybacon) | Software Engineer | newtelligenceR AG
blog: http://staff.newtelligence.net/danielf
usergroup: http://vfl-niederrhein.net


-----Original Message-----
From: ad [private.php?do=newpm&u=]
Posted At: Thursday, February 23, 2006 1:28 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to read a text file into a string variable
Subject: How to read a text file into a string variable

I have a text file in the directory of my web application.
How can I read this text file into a string vaiable?


 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      02-23-2006
> public string ReadFromFile(string filename)
> {
> StreamReader sr = File.OpenText(filename);
>
> StringBuilder sb = new StringBuilder();
> string str = sr.ReadLine();
> while( str != null )
> {
> sb.Append(str + "\n");
> str = sr.ReadLine();
> }
>
> sr.Close();
> return sb.ToString();
> }
>


or use ReadToEnd() :

string str;
using (StreamReader sr = File.OpenText(filename))
{ str = sr.ReadToEnd(); }


Hans Kesting


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a text file into an "insert into ..." file kublaikhan55@hotmail.com Ruby 5 07-23-2006 07:35 PM
Stupid Q: Read complete file into variable or string Franzl Wisseworst Perl Misc 15 07-09-2006 11:35 PM
Read Text File and split them to individual text file Krish ASP .Net 1 10-20-2005 03:39 PM
Read text file into std::string. Jason Heyes C++ 23 06-10-2005 03:46 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57