Category Archive: VB.NET Code Snippets

Function to Write to Application Event Log using VB.Net

Public Function WriteToEventLog(ByVal Entry As String) Dim appName As String = “Your Application” Dim eventType As EventLogEntryType = EventLogEntryType.Error Dim logName = “Application” Dim objEventLog As New EventLog() Try If Not EventLog.SourceExists(appName) Then EventLog.CreateEventSource(appName, logName) End If objEventLog.Source = appName objEventLog.WriteEntry(Entry, eventType) Return True Catch Ex As Exception Return False End Try End Function

How to Get Logged on UserName in VB.Net

MsgBox(Environment.UserName)

How to get ComputerName using Vb.Net

   MsgBox(My.Computer.Name.ToString)  

How to Read and Parse CommandLine Arguments in VB.NET

  Dim myCommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs         For i As Integer = 0 To myCommandLineArgs.Count – 1             MessageBox.Show(myCommandLineArgs(i))         Next Parameters are seperated by space.