Ping an IP address

My.Computer.Network.Ping(targetSystem[, timeout])

targetSystem is a string IP address, a host name, or a System.Uri instance. The optional timeout argument is supplied in milliseconds and defaults to 500.

This method returns true if the ping is successful, or False on failure or no response.

To ping localhost for example you would have the following

My.Computer.Network.Ping(127.0.0.1timeout])

Does a directory exist

Create a new Windows Forms application, and add a TextBox control named TextBox1 and a Button control named Button1 to the form. Now add the following code:

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       If (My.Computer.FileSystem.DirectoryExists(TextBox1.Text)) Then           MsgBox("The directory already exists.")        [...]

Number of days in a month

Dim daysInMonth As Integer = Date.DaysInMonth(Now.Year, Now.Month)

MsgBox(String.Format("Number of days in the current month: {0}", daysInMonth))

Is this a leap year

Dim leapYear As Boolean = Date.IsLeapYear(Now.Year)

MsgBox(String.Format("{0} is a leap year: {1}", Now.Year, leapYear))

Current date and time

Dim rightNow As Date = Now Dim result As New  System.Text.StringBuilder

result.Append("Date: ").AppendLine(rightNow.ToShortDateString) result.Append("Time: ").AppendLine(rightNow.ToShortTimeString) MsgBox(result.ToString())

Get the screen size

Module Module1

Sub Main()
Dim height As Integer = My.Computer.Screen.Bounds.Height
Dim width As Integer = My.Computer.Screen.Bounds.Width
Console.WriteLine(width & ” by ” & height)
Console.ReadLine() ‘wait for user input
End Sub

End Module

list serial ports

Module Module1

Sub Main()
For Each portName As String In My.Computer.Ports.SerialPortNames
Console.WriteLine(portName)
Next
Console.ReadLine() ‘wait for user input

End Sub

End Module

Is network available

Module Module1

Sub Main()

Dim isAvailable As Boolean
isAvailable = My.Computer.Network.IsAvailable
If (isAvailable) Then
Console.WriteLine(“Network is available”)
End If
Console.ReadLine() ‘Wait for key to be pressed
End Sub

End Module

Create a folder

This example shows how to create a folder

Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objDir As New DirectoryInfo(“d:\testfolder”)
Try
objDir.Create()
MsgBox(“Successful creation”)
Catch
MsgBox(“Failed to create folder”)
End Try

End Sub

End Class

A filestream example

opening and reading a file using the FileStream

 

Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objStream As New IO.FileStream(“d:\BOOT.txt”, IO.FileMode.Open)
Dim objReader As New IO.StreamReader(objStream)
Dim strBuffer As String
strBuffer = objReader.ReadToEnd
objReader.Close()
objStream.Close()
MsgBox(strBuffer)
End Sub

End Class