Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read a text file in ASP.NET

    Hi! Thanks and please give me some ideas on this:

    I want to make a Web page, with two windows (such as 2 textareas). One is for user to enter an asp .net file, such as
    <html>
    ....
    </html>

    The other is to display the result as in a browser.

    I am using VB .NET to write this. I am thinking
    1. I should read the textarea content into a ~.aspx.
    2. Then display it in an iframe by using src=~.aspx

    Should StreamReader and StreamWriter be introduced? Or there should be a better way?

    So do I need to make sure that the server that I am uploading this file to can interpret ASP.NET?

    Thank you so much for your input.

  • #2
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here is what I have already done. What's wrong?

    If anyone can shed some light on what I have already done. Right now what's being changed by user in one window, is not registered. Thanks.

    This is ASP .NET, written in VB .NET

    Imports System.IO

    Partial Class _Default
    Inherits System.Web.UI.Page

    Dim fileStreamReader As StreamReader
    Dim newfileStreamReader As StreamReader
    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim fileStreamWriter As StreamWriter
    Dim aString As String
    Dim oldFile As String = "old.htm"
    Dim newFile As String = "new0.htm"


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Read_Write_Record()
    End Sub
    Private Sub Read_Write_Record()
    With Me
    .contentLabel.Text = ""
    .TextBox1.Text = ""
    .Label1.Text = "oldFile is " & oldFile
    .Label2.Text = "newFile is " & newFile
    .Label5.Text = ""
    End With

    aString = ""

    Try
    fileStreamReader = New StreamReader(Server.MapPath(oldFile))
    j = j + 1
    fileStreamWriter = New StreamWriter(Server.MapPath(newFile))
    i = i + 1
    While fileStreamReader.Peek <> -1
    aString = fileStreamReader.ReadLine()
    fileStreamWriter.WriteLine(aString)
    With Me
    .TextBox1.Text = .TextBox1.Text & aString & ControlChars.CrLf
    .contentLabel.Text = .contentLabel.Text & aString
    End With
    End While
    Catch ex As Exception
    Me.contentLabel.Text = "File doesn't exist!"
    Me.Label5.Text = "i = " & i & "j = " & j
    End Try
    fileStreamReader.Close()
    fileStreamWriter.Close()
    oldFile = newFile
    newFile = "new" & i.ToString & ".htm"
    With Me
    .Label3.Text = "oldFile is " & oldFile
    .Label4.Text = "newFile is " & newFile
    End With
    End Sub

    Protected Sub ClickButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClickButton.Click
    Read_Write_Record()
    End Sub
    End Class

  • #3
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How to read your *.txt file:
    Code:
    Dim FileName As String = Server.MapPath("yourfile.txt")
    Dim FileContents As String
    Dim myStreamReader As StreamReader
    
    If Not FileName Is Nothing Then 
    	Try
    		myStreamReader = File.OpenText(FileName)
    		FileContents = myStreamReader.ReadToEnd()
    		lblContent.Text = FileContents
    	Catch myException As Exception
    		Response.Write("Error...")
    	Finally
    		myStreamReader.Close()
    	End Try
    End If
    How to write in your *.txt file:
    Code:
    Dim str As String = "bla blah..."
    Dim sw As StreamWriter
    Try
    	sw = File.CreateText(Server.MapPath("yourfile.txt"))
    	sw.Write(str)
    Catch myException As Exception
    	Response.Write("Error...")
    Finally
    	sw.Close()
    End Try

  • #4
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Otaku149,

    Thank you! It is really cool that you replied. I started thinking no one would. I can't wait to try it.

    I have another question. My design is to have 2 windows. I want to display a text file in the left window, and to display it as a browser would to the right window. My current code works:

    With Me
    ...
    .contentLabel.Text = .contentLabel.Text & aString
    End With


    But I don't really know why. Is it because the ".contentLabel.Text & aString" is naturally being displayed in a browser? I am using VB .NET to write ASP .NET.

    Thank you so much again!

  • #5
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    coolrunning,

    I thought you wanted to create/read a ~.txt file and after reading your original post it is important to mention that what you want to do is dangerous. Allowing anyone to run an ~.aspx is very dangerous because he can run server code and for example he can delete all your website or something.

  • #6
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks Otaku149

    Dear Otaku149,

    What I am trying to do is to create a tutorial for my beginning students for JavaScript and ASP.NET. So the left window will be the text file code, such as

    <html>
    <body>
    <script>document.write("<h1>This is a test.</h1>");</script>
    </body>
    </html>

    And the right hand window would display

    This is a test.

    User can change the text file to make it display differently.

    If the left hand side has ASP .NET file, I just want it to be displayed as a server-processed file, but not to endanger the system, any way to avoid it?

    Thanks so much!

  • #7
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I understand exactly your goal coolrunning but it's too dangerous because you want to let anyone run server side script. People will be able to read, write and delete your website files. You give to others the same permission as you have. Believe me, do not do that except if you want to be hacked.

  • #8
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Output

    Thanks Otaku149.

    Can you or someone tell me:

    This is a 1 row 2 cols table. In the left cell, I use a textbox for input. In the right cell, I just let the browser do the displaying (rendering) of the text.

    I find the dispaly area (the window to the right) get longer and longer, if the user enters a lot of text in the input area (the window to the left). The left window is a textbox, with a nice scrolling bar. The right window, unfortunately, just keeps growing as the input on the left window increases.

    I tried frameset, but the ASP .NET in VB .NET did like it. I want to use textarea, which will give me the automatic scrolling bar. But I can't get the display like I had before with the regular table cell.

    How do I get it, the right window, to display as it already has, as a browser, but has a scrolling bar when it gets too long? Thanks so much for looking into this.

  • #9
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read Text File

    You can read it very simple way in vb.net

    http://vb.net-informations.com/files...TextReader.htm

    thanks
    rbt.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •