Wednesday, November 16, 2011

Wednesday 11.16.11

Partial Class FileIO
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (Directory.Exists("C:\ASPFiles")) Then
Directory.CreateDirectory("C:\ASPFiles")
End If
Dim fStream As New FileStream("C:\ASPFiles\myFile.txt", FileMode.Create)
Dim w As New StreamWriter(fStream)

w.WriteLine(Now.ToString)
w.WriteLine("ASP.NET Text File Text") 'write a string
w.WriteLine(1000)

'tidy up
w.Flush()
w.Close()
End Sub

Imports System.IO

Partial Class FileIOLoad
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim r As StreamReader = File.OpenText("C:\ASPFiles\myFile.txt")
Dim line As String
Do
line = r.ReadLine()
If line IsNot Nothing Then
Response.Write(line)
End If
Loop While line IsNot Nothing

End Sub



Protected Sub cmdUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
Try
If Uploader.PostedFile.ContentLength > 11064 Then
'this exceeds the size limit you want to allow
'you should check the size to prevent a denial of service attack that attempt to fill up
'your web servers hard drive
'you might also want to check the amount of remaining free space
lblStatus.Text = "Too lage. This file is not allowed"
Else
'retireve the physical direcotry path for the upload subdirectory
Dim destDir As String = "C:/ASPFiles"
'Dim destDir As String = Server.MapPath("./Upload")
'extract the filename part from teh full path fo the original file
Dim fName As String = Path.GetFileName(Uploader.PostedFile.FileName)
'combine the destination directory wtih the filename
Dim destPath As String = Path.Combine(destDir, fName)
'save the file on the server
Uploader.PostedFile.SaveAs(destPath)
lblStatus.Text = "Thanks for submitting your file."
End If
Catch err As Exception
lblStatus.Text = err.Message
End Try

End Sub


Imports System.IO

Partial Class LogFile
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
Log("Page loaded for the first time")
Else
Log("Page posted back")
End If
End Sub

'store a user-specific log

Private Function GetFileName() As String
'crate a unique filename
Dim fileName As String = "user." & Guid.NewGuid().ToString()

'put the file in the current web applciation path
Return Path.Combine(Request.PhysicalApplicationPath, fileName)

End Function

Private Sub Log(ByVal message As String)
'check for the file
Dim mode As FileMode
Try
If ViewState("LogFile") Is Nothing Then
'first create a unique user-specific filename
ViewState("LogFile") = GetFileName()
'the log file must be greated
mode = FileMode.Create

Else
'add to the existing fle
mode = FileMode.Append
End If

'write teh message
'A Using block ensures the file is automatically closed
'even in the case of error
Dim fileName As String = CStr(ViewState("LogFile"))
Using fs As New FileStream(fileName, mode)
Dim w As New StreamWriter(fs)
w.WriteLine(DateTime.Now)
w.WriteLine(message)
w.Close()
End Using
Catch ex As Exception
Response.Write(ex.ToString())
End Try

End Sub


Protected Sub cmdReadLog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdReadLog.Click
If ViewState("LogFile") IsNot Nothing Then
Dim fileName As String = CStr(ViewState("LogFile"))
Using fs As New FileStream(fileName, FileMode.Open)
Dim r As New StreamReader(fs)
'read line by line allows you to add line breaks to the web page
Dim line As String
Do
line = r.ReadLine()
If line IsNot Nothing Then
lblInfo.Text &= line & "
"
End If
Loop While line IsNot Nothing
r.Close()
End Using
End If
End Sub
End Class

No comments: