Wednesday, September 7, 2011

Wednesday 9.7.11

Imports System
Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Namespace Apress.VisualBasicRecipes.Chapter05
Public Class Recipe05_09
Public Shared Sub Main()

'create a sample log file
Using w As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("C:\inetpub\SampleLog.txt", False, System.Text.Encoding.UTF8)
'write sample log records to the file the parser will skip blank lines. also the TextFieldParser can be configured to
'ignore lines that are comments.
w.WriteLine("# In this sample log file, coments start with a # character.")
w.WriteLine("# The parser, when configured correclty, will ignore these lines.")
w.WriteLine("")
w.WriteLine("{0}, INFO, ""{1} """, DateTime.Now, "Some informational text.")
w.WriteLine("{0}, WARN, ""{1} """, DateTime.Now, "Some warning message.")
w.WriteLine("{0}, ERR!, ""{1} """, DateTime.Now, "[ERROR] Some exception has occurred.")
w.WriteLine("{0}, INFO, ""{1} """, DateTime.Now, "More informational text.")
w.WriteLine("{0}, ERR!, ""{1} """, DateTime.Now, "[ERROR] Some exception has occurred.")
End Using

Console.WriteLine("Press enter to read and parse the information.")
Console.ReadLine()

'Open the file in and parse the data into a TextFieldParser object
Using logFile As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("C:\inetpub\SampleLog.txt")

Console.WriteLine("Parsing the text file")
Console.WriteLine(Environment.NewLine)

'write header informaton to the console
Console.WriteLine("{0, -29} {1} {2}", "Date/Time in RFC1123", "Type", "Message")

'Configure the parser. For this recipe, make sure HasFieldsEncolsedInQuotes is True.
logFile.TextFieldType = FieldType.Delimited
logFile.CommentTokens = New String() {"#"}
logFile.Delimiters = New String() {","}
logFile.HasFieldsEnclosedInQuotes = True

Dim currentRecord As String()

'loop through the file until we reach the end.
Do While Not logFile.EndOfData
Try
'Parse all the fields into the currentRow
'array This method automatically moves
'the file pointer to the next row.
currentRecord = logFile.ReadFields

'write the parsed record to the console.
Console.WriteLine("{0:r} {1} {2}", DateTime.Parse(currentRecord(0)), currentRecord(1), currentRecord(2))
Catch ex As MalformedLineException
'The MalformedLineException is thrown by the
'TextFieldParser anytime a line cannot be parsed.
Console.WriteLine("An exception occurred attempting to parse this row: ", ex.Message)
End Try
Loop
End Using

Console.WriteLine(Environment.NewLine)
Console.ReadLine()
End Sub

End Class
End Namespace


Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class EventsDemo : Inherits System.Windows.Forms.Form
Private btn As Button

Public Sub New()
btn = New Button()
btn.Location = New Point(50, 50)
btn.Text = "Test"

Controls.Add(btn)
AddHandler btn.Click, AddressOf btn_Click

End Sub

Public Shared Sub Main()
Application.Run(New EventsDemo())
End Sub

Private Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("btn_Click method ", "Events Demonstration")
End Sub
End Class


Imports System.Threading

Module Module1
Class MyEventArgs
Inherits System.EventArgs

Public Message As String
Public Time As DateTime

Public Sub New(ByVal s As String, ByVal dt As DateTime)
MyBase.New()
Message = s
Time = dt
End Sub
End Class

Class MyMonitor
Public Event EventStart(ByVal e As Object, ByVal args As MyEventArgs)
Public Sub GenerateEvent()
Dim Args As New MyEventArgs("Hacker, Hacker", Now())
RaiseEvent EventStart(Me, Args)
End Sub
End Class

Dim WithEvents HackerAlarm As New MyMonitor()
Dim attackNum As Integer = 1

Sub Attack(ByVal o As Object, ByVal args As MyEventArgs) Handles HackerAlarm.EventStart
Console.WriteLine("Hack attack in progress")
Console.WriteLine(args.Message)
Console.WriteLine(args.Time)
Console.WriteLine("Attack number {0}", attackNum)
attackNum = attackNum + 1

End Sub

Sub Main()
Dim i As Integer

Do While i < 10
HackerAlarm.GenerateEvent()
i += 1
Thread.Sleep(1100)
Loop


End Sub

End Module


Imports System
Imports System.Net
Imports System.IO
Imports System.Environment

Module GetURL
Sub Main()
Dim sOutput As String
Dim sURL As String = "http://www.java2s.com"
Try
Dim objNewRequest As WebRequest = HttpWebRequest.Create(sURL)
Dim objResponse As WebResponse = objNewRequest.GetResponse
Dim objStream As New StreamReader(objResponse.GetResponseStream())
sOutput = objStream.ReadToEnd()

Catch eUFE As UriFormatException
sOutput = "Error in URL Format: [" & sURL & "]" & NewLine() & eUFE.Message
Catch ex As Exception
sOutput = ex.ToString
Finally
Console.Write(sOutput)
End Try
End Sub
End Module


Module Tester
Sub Main()
Dim i As Integer
Dim array As Integer() 'declare array variable
array = New Integer(9) {}

Console.WriteLine("Subscript " & vbTab & "Value")

For i = 0 To array.GetUpperBound(0)
Console.WriteLine(i & vbTab & vbTab & array(i))
Next

Console.WriteLine("The array contains " & array.Length & " elements.")
End Sub
End Module

No comments: