Uptime with PerformanceCounters

Sub Main()

        Dim arguments As String() = Environment.GetCommandLineArgs()

        Dim b64 As Boolean = False

        Dim bRun As Boolean = True

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

        Dim oldColor As ConsoleColor = Console.ForegroundColor

        'If LogonUser("user", "domain", "password", 2, 0, tokenHandle) Then

        '    'Console.WriteLine("Logon Successful")

        '    newId = New WindowsIdentity(tokenHandle)

        '    impersonatedUser = newId.Impersonate()

        '    Console.ForegroundColor = ConsoleColor.DarkCyan

        '    Console.WriteLine(funFormatColumns("", 45, True) & funFormatColumns("User: " & newId.Name.ToString, 30, False))

        'Else

        '    Console.ForegroundColor = ConsoleColor.Red

        '    Console.WriteLine("Logon Failed")

        'End If

        'Console.WriteLine("User: " & newId.Name.ToString)

        If arguments.Length > 1 Then

            pc.MachineName = arguments(1)

            Console.ForegroundColor = ConsoleColor.Blue

            Console.WriteLine("Machine: " & pc.MachineName.ToString)

            Console.WriteLine()

            If Not Valid_Ping(pc.MachineName.ToString) Then bRun = False

        End If

        If bRun Then

            ' Console.WriteLine(">>> " & pc.MachineName.ToString)

            Try

                pc.NextValue() ' This returns zero for a reason I don't know

                If pc.MachineName.ToString = "." Then

                    If Directory.Exists("c:\program files (x86)") Then

                        b64 = True

                    End If

                Else

                    If Directory.Exists("\\" & pc.MachineName.ToString & "\c$\program files (x86)") Then

                        b64 = True

                    End If

                End If

                ' This call to NextValue gets the correct value

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

                'Console.WriteLine(duration.ToString)

                Console.ForegroundColor = ConsoleColor.Green

                Console.Write(funFormatColumns(duration.Days & " Days " & duration.Hours & " Hours " & duration.Minutes & " Minutes " & duration.Seconds & " Seconds", 45, True))

                'Console.WriteLine()

                Console.ForegroundColor = ConsoleColor.Gray

                Console.WriteLine(funFormatColumns("(" & DateTime.Now.Subtract(duration) & ")", 33, False))

            Catch e As Exception

                Console.ForegroundColor = ConsoleColor.Red

                Console.WriteLine("Failed: " & e.Message.ToString)

            End Try

            'Catch

            'End Try

        End If

        'impersonatedUser.Undo()

        'impersonatedUser.Dispose()

        'newId.Dispose()

        'CloseHandle(tokenHandle)

        ' newId = WindowsIdentity.GetCurrent

        'Console.ForegroundColor = ConsoleColor.DarkCyan

        'Console.WriteLine(funFormatColumns("", 45, True) & funFormatColumns("User: " & newId.Name.ToString, 30, False))

        Console.ForegroundColor = oldColor

        'Console.ReadLine()

    End Sub


Valid_Ping


    Private Function Valid_Ping(ByVal SystemName As String) As Boolean

        Dim PingReplied As Boolean = False

        Try

            Dim PingSender As New Ping

            Dim Options As New PingOptions

            ' Use default TTL of 128

            ' Change to not fragment

            Options.DontFragment = True

            ' Create 32 byte data buffer to send

            Dim PingData As String = "******Computer*****Details******"

            Dim Pingbuffer() As Byte = Encoding.ASCII.GetBytes(PingData)

            Dim PingTimeout As Integer = 120

            Dim PingReply As PingReply = PingSender.Send(SystemName, PingTimeout, Pingbuffer)

            If PingReply.Status = IPStatus.Success Then

                PingReplied = True

            Else

                PingReplied = False

            End If

            Return PingReplied

        Catch ex As Exception

            Return PingReplied

        End Try

    End Function

 IP Check RegEx (VBscript)

wscript.echo IP_Valide("10.12.2.125")

Function IP_Valide(ip)

   IP_Valide=false

    Set myRegExp = New RegExp

     myRegExp.IgnoreCase = True

     myRegExp.Global = True

     myRegExp.Pattern ="^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"

     set myMatches =  myRegExp.execute(ip)

    'IP_Valide= myRegExp.execute( ip)

     For Each myMatch in myMatches

        'msgbox myMatch.Value, 0, "Found Match"

        IP_Valide=true

     Next

End function

Remove HTML characters from string

    'https://stackoverflow.com/questions/17665582/stripping-out-html-tags-in-string

<Extension()> Public  Function funRemoveHTML(sourcestring As String) As String

        'Dim sourcestring As String = "replace with your source string"

        Dim replacementstring As String = ""

        Dim matchpattern As String = "<(?!br)(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>"  'leave the <br> tag

        'Dim matchpattern As String = "<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>" 'removes all html

        'For the return value, making <br> into vbCrLf

        Return Replace(Replace((Regex.Replace(sourcestring, matchpattern, replacementstring, RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace Or RegexOptions.Multiline Or RegexOptions.Singleline)), "<br />", vbCrLf), "<BR>", vbCrLf)

    End Function

IsFileOpen

' Jeremy Thompson's code from here

    Private Sub IsFileOpen(ByVal file As FileInfo)

        Dim stream As FileStream = Nothing

        Try

            Debug.Print("Testing " & file.FullName.ToString)

            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)

            stream.Close()

        Catch ex As Exception

            If TypeOf ex Is IOException Then 'AndAlso IsFileLocked(ex) Then

                ' do something here, either close the file if you have a handle, show a msgbox, retry  or as a last resort terminate the process - which could cause corruption and lose data

                'Form1.ListView1.Items.Add("******" & file.FullName.ToString)

                Debug.Print("******" & file.Name.ToString)

                ListViewAddItem("******" & file.Name.ToString, "-", "-")

                Application.DoEvents()

            End If

        End Try

    End Sub

https://stackoverflow.com/questions/36950966/detect-if-any-file-in-use-by-other-process-of-a-directory-in-vb