ASP.NET Web Pages files
This chapter describes the knowledge about working with text files.
Using a text file
In the previous chapter, we have learned that web page data is stored in the database.
You can also put the site data is stored in a text file.
Text used to store data files are commonly called flat files. Common text file format is .txt, .xml and .csv (comma separated values).
In this chapter, you will learn to:
- How to read from a text file and display the data
Add a text file manually
In the following example, you will need a text file.
On your website, if no App_Data folder, create one. In the App_Data folder, create a file named Persons.txt of.
Add the following to the file:
Persons.txt
Steven,Spielberg
Alfred,Hitchcock
Data show that in the text file
The following example demonstrates how to display data in a text file:
Examples
var dataFile = Server.MapPath("~/App_Data/Persons.txt");
Array userData = File.ReadAllLines(dataFile);
}
<!DOCTYPE html>
<html>
<body>
<h1>Reading Data from a File</h1>
@foreach (string dataLine in userData)
{
foreach (string dataItem in dataLine.Split(','))
{@dataItem <text> </text>}
<br />
}
</body>
</html>
Running instance »
Examples explained
Use Server.MapPath find the exact path to the text file.
Use File.ReadAllLines open the text file and reads the file into an array of all rows.
Data array for each row of data in a data item is displayed.
Data show that the Excel file
Use Microsoft Excel, you can save a spreadsheet as a comma-delimited text file (.csv file). In this case, each row in the spreadsheet is saved as a text line, each data column separated by commas.
in the example above can be used to read a Excel .csv file (just the name of the file name into the corresponding Excel files).