Viewing source for Recipe1714cs.aspx
<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
protected void Page_Load (object Source, EventArgs e)
{
Line.Text = GetLineFromFile(Server.MapPath("starwarquotes.txt"), 15);
}
public string GetLineFromFile(string filename, int lineNumber)
{
string result = string.Empty;
StreamReader sr = null;
try
{
int lineCount = 1;
sr = File.OpenText(filename);
string str = sr.ReadLine();
while( str != null )
{
if (lineCount == lineNumber)
{
result = str;
break;
}
str = sr.ReadLine();
lineCount++;
}
if (lineNumber > lineCount)
{
result = "You entered a line number greater than the number of lines in the file.";
}
}
catch(Exception ex)
{
result = ex.Message;
}
finally
{
if (sr != null) sr.Close();
}
return result;
}
</script>
<html>
<body>
<form runat="server">
<asp:Label id="Line" runat="server" />
</form>
</body>
</html>