Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I store text file text into String variable?

Status
Not open for further replies.

aspdotnetuser

Programmer
Oct 10, 2008
45
GB
Hi,

This is probably a simple task but I'm having trouble trying to get text file text into a variable.

I've searched on the Internet and have found some examples but I don't seem to be able to get them to work, can someone show me the correct way of doing this?

 
I actually found the answer to this question before I submitted it! I thought I would post it anyway because it seems like it might be a common question for newbie programmers.

I found the easiest way to store text file text into a String variable is to use the following line of code:

[blue]string s = System.IO.File.ReadAllText( path );[/blue]

[red]Note: If you using a path like c:\folder\folder\filename.txt and it doesn't like the path becauseof the slashes, put an @ symbol at the front of the string:[/red]

[blue]string s = System.IO.File.ReadAllText(@"c:\folder\folder\....");[/blue]

Here's the url where I found it:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top