Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET Web Pages file


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Pages - File

ASP.NET you can store data in a Web Pages file, as long as you need to add a text file, and you'll learn more about Web Pages files below.


This chapter describes the use of text files.


Use a text file

In the previous sections, we learned that web page data is stored in a database.

You can also store site data in a text file.

Text files used to store data are often referred to as flat files. Common text file formats are .txt, .xml, and .csv (comma-separated values).

In this chapter, you'll learn:

  • How to read and display data from a text file

Add a text file manually

In the following example, you will need a text file.

On your site, if you don't App_Data folder, create one. In App_Data folder, create a file called Persons .txt file.

Add the following to the file:

Persons.txt

George,Lucas
Steven,Spielberg
Alfred,Hitchcock


Displays the data in the text file

The following example shows how to display data in a text file:

@{
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>&nbsp;</text>}

<br />
}
</body>
</html>

Run an instance . . .

The instance explanation

Use Server.MapPath to find the exact path to the text file.

Use File.ReadAllLines to open a text file and read all the lines in the file into an array.

The data for the data items in each data row in the array is displayed.


Displays the data in the Excel file

With Microsoft Excel, you can save a spreadsheet as a comma-separated text file .csv file. At this point, each row in the spreadsheet is saved as a line of text, and each data column is separated by a comma.

in you can use the instance above to read an Excel .csv file (just change the file name to the name of the corresponding Excel file).