前台代碼:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 269px; height: 75px">
<tr>
<td align="center" style="font-weight: bold;
font-size: 30px; width: 237px; color: lime; background-color: gray;">
網站訪問量</td>
</tr>
<tr>
<td align="center" style="width: 237px; background-color: gray;">
你是第<asp:Label ID="onlineCount" runat="server" T
ext="" Width="62px"><%=Application["onlinecount"]%>
</asp:Label>位訪問者
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Global.asax代碼:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在應用程序啟動時運行的代碼
int count = 0;
StreamReader sdr;
// 獲取文件路徑
string filePath = Server.MapPath("count.txt");
// 打開文件
sdr = File.OpenText(filePath);
// 讀取文件
while(sdr.Peek()!=-1)
{
string str = sdr.ReadLine();
// 把字符串強制類型轉換成整型數據
count = int.Parse(str);
}
sdr.Close();
object objcount = count;
Application["onlinecount"] = count;
}
void Application_End(object sender, EventArgs e)
{