Site Search:
Sign in | Join | Help
4Penny.net

VB.NET

Notes, Tricks and Tips on VB.NET
  • How to write to the application event log using VB.NET

    Here a sample of code that will write to the application event log, using VB..NET:

     

     Public Function WriteToEventLog(ByVal Entry As String)
    
    
        Dim appName As String = "Server Maintenance"
        Dim eventType As EventLogEntryType = EventLogEntryType.Error
        Dim logName = "Application"
    
    
        Dim objEventLog As New EventLog()
    
    
        Try
            'Register the App as an Event Source
            If Not EventLog.SourceExists(appName) Then
                EventLog.CreateEventSource(appName, logName)
            End If
    
    
            objEventLog.Source = appName
    
    
            'WriteEntry is overloaded; this is one
            'of 10 ways to call it
            objEventLog.WriteEntry(Entry, eventType)
            Return True
        Catch Ex As Exception
            Return False
    
    
        End Try
    
    
    End Function

  • Code to generate a random filename

    Here's a short function that will generate a random filename

     Private Function GetRandomFileName() As String
    
    
        Dim r As New Random()
        Dim i As Integer
        Dim strTemp As String = ""
    
    
        For i = 0 To 8
            strTemp = strTemp & Chr(Int((26 * r.NextDouble()) + 65))
        Next
    
    
        Return "c:\\" & strTemp & ".bmp"
    
    
    End Function

  • Processing arguments in a VB.NET console app

    Here is a VB.NET class that shows how to process arguments for a console app

    Create a console app, and in the project properties click on DEBUG, then specify something in the 'Commant Line Arguments' area. For the sample app, you might put 'olap diskspace' and those two items will be processed.

     Once you build and deploy the app, you would call the app like this: MYAPP.EXE OLAP DISKSPACE, where OLAP and DISKSPACE are my example arguments

    Module Module1
    
    
        Sub Main(ByVal args() As String)
            'can be called with three args:
            'OLAP - runs the olap scrips
            'DISKSPACE - checks the disk space
            'BACKUP - runs a file copy routine
    
    
            Dim a As Int16
            If args.Length = 0 Then
                Exit Sub
            End If
    
    
            For a = 0 To args.Length - 1
                Select Case args(a).ToUpper
                    Case "OLAP"
                        processOLAP()
                    Case "DISKSPACE"
                        processDISKSPACE()
                    Case "BACKUP"
                        processDISKSPACE()
                End Select
    
    
            Next
    
    
    
        End Sub
        Sub processOLAP()
    
    
        End Sub
        Sub processDISKSPACE()
    
    
        End Sub
        Sub processBACKUP()
    
    
        End Sub
    End Module

More Posts Next page »