。打开一个TXT文本的例子
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat=server>
void Page_Load(Object sender , EventArgs e)
{
StreamReader objStreamReader;
string strInput;
if ( File.Exists( MapPath( "myFile.txt" ) ) )
{
objStreamReader = File.OpenText( MapPath( "myFile.txt" ) );
strInput = objStreamReader.ReadLine();
while ( strInput != null)
{
lblContents.Text += "<li>" + strInput;
strInput = objStreamReader.ReadLine();
}
objStreamReader.Close();
}
else
{
lblContents.Text = "myFile.txt does not exist!";
}
}
</Script>
<html>
<head><title>OpenText.aspx</title></head>
<body>
<h1>Contents of myFile.txt</h1>
<asp:Label
ID="lblContents"
Runat="Server" />
</body>
</html>
向TXT文本文件写内容的例子
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat=server>
void Button_Click(Object sender , EventArgs e)
{
StreamWriter objStreamWriter;
objStreamWriter = File.CreateText( MapPath( "myFile.txt" ) );
objStreamWriter.WriteLine( txtInput.Text );
objStreamWriter.Close();
}
</Script>
<html>
<head><title>WriteText.aspx</title></head>
<body>
<form runat="Server">
<h1>Write to a Text File:</h1>
<br>
<asp:TextBox
ID="txtInput"
Runat="Server" />
<asp:Button
Text="Write!"
OnClick="Button_Click"
Runat="Server" />
</form>
</body>
</html>
往txt文本文件里面加内容例子
<script language="C#" runat=server>
void Button_Click(Object sender , EventArgs e)
{
StreamWriter objStreamWriter;
objStreamWriter = File.AppendText( MapPath( "myFile.txt" ) );
objStreamWriter.WriteLine( txtInput.Text );
objStreamWriter.Close();
}
</Script>
<html>
<head><title>AppendText.aspx</title></head>
<body>
<form runat="Server">
<h1>Append to a Text File:</h1>
<br>
<asp:TextBox
ID="txtInput"
Runat="Server" />
<asp:Button
Text="Append Text!"
OnClick="Button_Click"
Runat="Server" />
</form>
</body>
</html>
<%@ Import Namespace="System.IO" %>