Jump to content


Check out our Community Blogs

blindtrevor's Content

There have been 6 items by blindtrevor (Search limited from 27-May 19)


Sort by                Order  

#573935 How to create a Bandwidth Monitor

Posted by blindtrevor on 20 September 2010 - 08:13 AM in Visual Basic Tutorials

I've found an even better way of doing this - less glitchy and true .NET:

Imports System.Net.NetworkInformation
Then replace the whole objProcess bit with this:
Try
            Dim netInterface As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces
            For Each net As NetworkInterface In netInterface
                Dim statistics As IPv4InterfaceStatistics = net.GetIPv4Statistics
                If net.Name <> "MS TCP Loopback interface" Then
                    If net.OperationalStatus = 1 Then
                        DLoad = statistics.BytesReceived
                        ULoad = statistics.BytesSent
                    End If
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, ex.InnerException)
        End Try



#573697 How do I convert the result of a msgbox to a textbox

Posted by blindtrevor on 17 September 2010 - 05:45 AM in Visual Basic

Or:
Function roundBytes(ByVal bytes As String)
        If bytes > 1073741824 Then
            Return Round(bytes / 1073741824, 2) & " GB"
        ElseIf bytes > 1048576 Then
            Return Round(bytes / 1048576, 2) & " MB"
        ElseIf bytes > 1024 Then
            Return Round(bytes / 1024, 2) & " KB"
        Else
            Return bytes & " bytes"
        End If
    End Function

Make sure you add
Imports System.Math
at the top :)



#573696 Reading a text file and saving to settings.

Posted by blindtrevor on 17 September 2010 - 05:42 AM in Visual Basic

Hmmm... actually - this will just save each line to Setting1... Hmmmm?

How about domething like this...?

Imports System.IO

Public Class Form1


Dim FileReader As StreamReader
Dim fileLocation As String = "c:\text.txt"


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     If My.Computer.FileSystem.FileExists(fileLocation) Then
            FileReader = New StreamReader(fileLocation)
            Dim strLine As String
            Do
                strLine = FileReader.ReadLine
                If strLine <> Nothing Then
                    My.Settings.Setting1 = strLine
                    My.Settings.Save()
                End If
            Loop While strLine <> Nothing
            FileReader.Close()
        End If
End Sub
End Class




#573695 Reading a text file and saving to settings.

Posted by blindtrevor on 17 September 2010 - 05:39 AM in Visual Basic

How about domething like this...?

Imports System.IO

Public Class Form1


Dim FileReader As StreamReader
Dim fileLocation As String = "c:\text.txt"


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     If My.Computer.FileSystem.FileExists(fileLocation) Then
            FileReader = New StreamReader(fileLocation)
            Dim strLine As String
            Do
                strLine = FileReader.ReadLine
                If strLine <> Nothing Then
                    My.Settings.Setting1 = strLine
                    My.Settings.Save()
                End If
            Loop While strLine <> Nothing
            FileReader.Close()
        End If
End Sub
End Class



#573691 [Help]Save value in textbox?

Posted by blindtrevor on 17 September 2010 - 05:26 AM in Visual Basic

You can savve settings like this inside the EXE.

Try taking a look at my.settings

You need to go into "My Project" (in the sollution explorer window) and then click on the settings tab to set up all your different settings.

Then you can say something like:
for example, if you created a new setting called strTextBox...
My.Settings.strTextBox = TextBox1.text
My.Settings.Save
Then in the form.load section you can put:
TextBox1.text = My.Settings.strTextBox

there's no need to put things in the registry if you can help it :)

Any probelms, give me a shout :)


Andrew



#573595 How to create a Bandwidth Monitor

Posted by blindtrevor on 16 September 2010 - 05:10 AM in Visual Basic Tutorials

Hi,

There is a slightly neater way of getting the NetStat -e as a variable without having to constantly write and read to the disk (keep everything in memory)

Microsoft aren't really that fond of using the Shell command... instead they like you to use a process and pass the results through to a variable.

The way to do this in this instance is like so:

First create a new form with a timer (timer1) set to 1000 interval, a text box (TextBox1) set to multiline, a button (Button1) and a label (Label1).

Now you have that we can put the code in:

'----------------------------------------------------------------------------------------------------------------------------
'Script Name : BandwidthMonitor.sln
'Author      : Andrew Samuel
'Created     : 16/09/2010
'Description : Tracks the inbound and outbound traffic on a PC
'----------------------------------------------------------------------------------------------------------------------------
'We need to import System.Math to allow a bit of rounding
'----------------------------------------------------------------------------------------------------------------------------
Imports System.Math

Public Class Form1
    '----------------------------------------------------------------------------------------------------------------------------
    'Here we declare all our variables
    '----------------------------------------------------------------------------------------------------------------------------
    Dim objProcess As New Process               'Used to process the netstat command
    Dim strOutput As String                     'A string to store the output of the netstat process
    Dim strError As String                      'A string to store any errors of the netstat process
    Dim DLoad As Long                           'This will be the raw download data from netstat
    Dim ULoad As Long                           'This will be the raw upload data from netstat
    Dim LastULoad As Long = 0                   'This will be used to calculate the surrent usage
    Dim LastDLoad As Long = 0                   'This will be used to calculate the current usage
    Dim SString As String()                     'We need this string array to give us the ability to get the data we need from the netstat command
    Dim doAfterSecondPass As Boolean = False    'This we will use to allow the program to process something on and after the second pass through

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()                          'This will start polling the netstat each second
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This is where the fun starts!
        '
        'We need to set all the variables inside the objProcess
        objProcess.StartInfo.RedirectStandardOutput = True
        objProcess.StartInfo.CreateNoWindow = True
        objProcess.StartInfo.RedirectStandardError = True
        objProcess.StartInfo.FileName() = "netstat"
        objProcess.StartInfo.Arguments() = "-e"
        objProcess.StartInfo.UseShellExecute = False
        'Now we will start the process
        objProcess.Start()
        'Here we will store the outputs to our 2 strings
        strOutput = objProcess.StandardOutput.ReadToEnd
        strError = objProcess.StandardError.ReadToEnd
        'And then we wait for the process to end before carrying on
        objProcess.WaitForExit()
        'Now we output the information we have
        TextBox1.Text = strOutput 'This is the raw data from netstat -e
        SString = Split(strOutput, "Bytes") 'We split that string at the word Bytes
        DLoad = Int(Mid(SString(1), 14, 17)) 'Find the download figure and store it to DLoad variable
        ULoad = Int(Mid(SString(1), 31, 16)) 'Find the upload figure and store it to ULoad variable

        'Here is where we work out how much data has passed in the last second
        If doAfterSecondPass = True Then
            Label1.Text = "Upload: " & roundBytes(ULoad - LastULoad) & "/s" & vbCrLf & "Download: " & roundBytes(DLoad - LastDLoad) & "/s"
        Else
            doAfterSecondPass = True
        End If
        'And this is where we set the las values ready to use next time
        LastDLoad = DLoad
        LastULoad = ULoad
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    'This is the function to return a slightly friendlier value for the bytes we got from netstat
    '----------------------------------------------------------------------------------------------------------------------------
    Function roundBytes(ByVal bytes As String)
        If bytes > 1073741824 Then
            Return Round(bytes / 1073741824, 2) & " GB"
        ElseIf bytes > 1048576 Then
            Return Round(bytes / 1048576, 2) & " MB"
        ElseIf bytes > 1024 Then
            Return Round(bytes / 1024, 2) & " KB"
        ElseIf bytes = 0 Then
            Return bytes & " B"
        Else
            Return bytes & " B"
        End If
    End Function

End Class

I've tried to comment as much as I could.

I hope this is helpful to someone.

Attached Files





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download