|
|
|
|
||||||||||||
|
Click here to return to my article index
Updated: 9/16/2002 - Thanks to Charles C., Paul M., and Joel M. for pointing out the faulty code. Note: This is not the only technique to open a text file and my not be the preferred technique given certain circumstances. The code below opens a text file (words.txt) and uses the File class to open a stream using the StreamReader class. We use the StreamReader class to loop through each line in the file, appending (Best Practice Alert! Read about string concatenation best practices here.) it to the string variable myOutputString as we go.
<%@ Page Language="VB" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<html>
<script language="VB" runat=server>
Sub Page_Load(source As Object, E As EventArgs)
Dim FileName As String = Server.MapPath("words.txt")
Dim FileContents As String
Dim myStreamReader As StreamReader
Try
myStreamReader = File.OpenText(strFile)
FileContents = myStreamReader.ReadToEnd()
Label1.Text = FileContents
Catch myException As Exception
If Not myStreamReader Is Nothing Then
myStreamReader.Close()
End If
End Try
End Sub
</script>
</html>
Result:This is a test file with some some repeated words that the program will insert into an Label Control | ||||||||||||||
| Copyright © 2000-2003 ASPAlliance.com Page Rendered at
10/7/2008 12:27:56 PM |
||||||||||||||