Random Number Function in Range

    Function funRandomNumber(lowerbound As Integer, upperbound As Integer) As Integer

        funRandomNumber = Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)

    End Function


Disk Info

Imports System.Management

Imports System.Security.Principal

Imports Microsoft.Win32

    Function GetDisksInfo(sComputerNameToQuery As String) As String

        Dim objDiskDetails As ManagementObjectSearcher

        Dim objMgmt As ManagementObject

        Dim scope As ManagementScope

        Dim options As New ConnectionOptions

        options.Username = "elevatedUser"

        options.SecurePassword = ToSecureString("password")

        scope = New ManagementScope("\\" & sComputerNameToQuery & "\root\cimv2", IIf(sComputerNameToQuery = ".", Nothing, options))

        scope.Connect()

        objDiskDetails = New ManagementObjectSearcher(scope, New ObjectQuery("Select * from Win32_LogicalDisk WHERE DriveType=3"))

        Dim pctFreeSpace, strFreeSpace, strusedSpace, strDiskSize

        Dim txt As String = ""

        Try

            For Each objMgmt In objDiskDetails.Get

                pctFreeSpace = Int((objMgmt("FreeSpace") / objMgmt("Size") * 1000) / 10)

                strDiskSize = Int(objMgmt("Size") / 1073741824) & ""

                strFreeSpace = Int(objMgmt("FreeSpace") / 1073741824) & ""

                strusedSpace = Int((objMgmt("Size") - objMgmt("FreeSpace")) / 1073741824) & ""

                txt += objMgmt("name") & vbTab & strDiskSize & vbTab & strusedSpace & vbTab & strFreeSpace & vbTab & pctFreeSpace & vbCrLf

            Next

        Catch

            txt += vbTab & "0" & vbTab & "0" & vbTab & "0" & vbTab & "0" & vbCrLf

        End Try

        GetDisksInfo = txt

    End Function

Chart - Example

Dim sqlQuery As String = ""

        'Debug.Print(Form1.cmbBank1.SelectedItem.Text)
        If iNum < 1 Then iNum = 25

        Dim iAvgOfAvg = funGetChartAverageOfAverages(iNum)
        sqlQuery = "select max(iValue) AS `TheMax`,min(iValue) AS `TheMin`,DATE(`dtDayTime`) AS `TheDate` from entries GROUP BY DATE(`dtDayTime`) ORDER BY DATE(`dtDayTime`) LIMIT " & iNum
        sqlQuery = "select AVG(iValue) AS `TheMax`,min(iValue) AS `TheMin`,DATE(`dtDayTime`) AS `TheDate` from entries GROUP BY DATE(`dtDayTime`) ORDER BY DATE(`dtDayTime`) DESC LIMIT " & iNum


        Dim SQLConnection As New MySqlConnection(connectionString)
        Dim sqlCommand As New MySqlCommand
        sqlCommand.Connection = SQLConnection

        Form1.Chart1.Series.Clear()
        Dim dv As DataVisualization.Charting.Series = Form1.Chart1.Series.Add("Blood Sugar")
        Form1.Chart1.ChartAreas(0).AxisX.Interval = 1
        Form1.Chart1.ChartAreas(0).AxisX.Minimum = 0
        Form1.Chart1.ChartAreas(0).AxisX.Maximum = iNum
        Form1.Chart1.ChartAreas(0).AxisX.IsReversed = True
        'Form1.Chart1.BorderlineColor = Color.Red
        dv.ChartType = SeriesChartType.Spline
        dv.LegendText = "Day of the Month x Sugar level"
        dv.LegendText = "Day of the Month x Avg Sugar level"

        Dim dvAvg As DataVisualization.Charting.Series = Form1.Chart1.Series.Add("Blood Sugar Avg")
        dvAvg.ChartType = SeriesChartType.Line
        'dv.LegendText = "Day of the Month x Sugar level"
        dvAvg.LegendText = iNum & " Day Average = " & iAvgOfAvg

Chart - Target Point Label

  Dim targetPoint As Int32
    targetPoint = Chart1.Series("Drawing").Points.AddXY(....)
    Then, use that targetPoint to edit the Label :

    Chart1.Series("Drawing").Points.Item(targetPoint).Label = "lalalilala"

Get Sizes (TB,GB,MB,KB,B)

Function GetSize(inSize As UInt32) As String
        Dim sReturn As String = "*"
        Dim iMB As Integer = 1024
        Dim aSize() As String = {" Bytes", " KB", " MB", " GB"}
        Dim iStep As Integer = 0
        Console.WriteLine(vbTab & inSize)

        Dim iCurrentSize As Decimal = inSize
        While iCurrentSize > iMB
            Console.WriteLine("iCurrentSize / iMB: " & iCurrentSize & " / " & iMB)
            iCurrentSize = iCurrentSize / iMB
            iStep += 1

        End While

        sReturn = Math.Round(iCurrentSize, 2) & aSize(iStep Mod 4)

        Return sReturn
    End Function

.

Delete Folders/Files Recursively

   Private Sub DeleteFilesRecursive(strFolder As String)

      For Each dirFolder In Directory.GetDirectories(strFolder)
         Directory.Delete(dirFolder, True)
      Next

      For Each file In Directory.GetFiles(strFolder)
         File.Delete(file)
      Next
   End Sub

Uptime for a server

    Function funGetUptime(sComputer As String) As String

        Dim pc As PerformanceCounter = New PerformanceCounter("System", "System Up Time")

        pc.MachineName = sComputer ' Must have permissions over the machine in order to gain this.

        Try

            pc.NextValue()

        Catch ex As Exception

            Return ex.Message.ToString

        End Try

        Dim duration As TimeSpan = TimeSpan.FromSeconds(pc.NextValue())

        Return (duration.Days & " Days " & duration.Hours & " Hours " & duration.Minutes & " Minutes " & duration.Seconds & " Seconds")

    End Function


DateTime.Now.Subtract(duration)


Example of File Version comparison

Dim oFileVer As FileVersionInfo = FileVersionInfo.GetVersionInfo(oFile.sPath)

  'If CDbl(Replace(oFile.sVersion, ".", ",")) >= CDbl(Replace(oFileVer.FileVersion, ".", ",")) Then

  If String.Compare(oFile.sVersion, oFileVer.FileVersion) <= 0 Then

   'we're ok

    funWriteWordinColor("Good version: (current) " & oFileVer.FileVersion & " >= (needed) " & oFile.sVersion & vbCrLf, ConsoleColor.Green)

   Else

      funWriteWordinColor("Incorrect version (" & oFile.sFileName & ") : (current) " & oFileVer.FileVersion & " < (needed) " & oFile.sVersion & vbCrLf, ConsoleColor.Red)

   End If