473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to read a text file into a string variable

ad
I have a text file in the directory of my web application.
How can I read this text file into a string vaiable?
Feb 23 '06 #1
3 2259
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" <fl****@wfes.tcc.edu.tw> wrote in message
news:uQ**************@TK2MSFTNGP11.phx.gbl...
I have a text file in the directory of my web application.
How can I read this text file into a string vaiable?

Feb 23 '06 #2
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 [mailto:fl****@wfes.tcc.edu.tw]
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?
Feb 23 '06 #3
> 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
Feb 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
3
by: ad | last post by:
I have a text file, it is some scripts of SQL. I want to read it into a variable, and set the string variable tto a command string of SqlCommand. How can I read a text file into a string...
0
by: TRB_NV | last post by:
I'm on a new hosted server platform which doesn't support PERL, so I'm converting some of my old code so it'll run as ASP. I have a routine I wrote in 1998 where I have a standard template Word...
3
by: John | last post by:
I have a text file that is formatted like this: 123456789 SOLD 1 12/12/2006 Smith 45,525 987654321 SOLD 3 01/01/2006 Jones 23,252
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
0
by: Snoze | last post by:
Hello, I'm trying to make an application with C# that writes and reads some lines of a text file, but I want to read one specific line and save the text in a string variable. And the inverse...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.