Some times we do need to find out the cpu’s id, which is unique for each CPU. This code example will help you to perform this task in vb.net.
To Execute the code, create a Visual Basic Windows Application in .Net; Add a button (“Button1”) in the form (“Form1”) then Implement the below given code in Form1.vb.
'Import following namespace
Imports System.Management
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare following three object variables
Dim objMOS As ManagementObjectSearcher
Dim objMOC As Management.ManagementObjectCollection
Dim objMO As Management.ManagementObject
'Now, execute the query to get the results
objMOS = New ManagementObjectSearcher("Select * From Win32_Processor")
objMOC = objMOS.Get
'Finally, get the CPU's id.
For Each objMO In objMOC
MessageBox.Show("CPU ID = " & objMO("ProcessorID"))
Next
'Dispose object variables
objMOS.Dispose()
objMOS = Nothing
objMO.Dispose()
objMO = Nothing
End Sub
End Class
Related posts
aby Uncategorized, vb.net
Here is a simple code to create a captcha image. This ASP.net code creates a bitmap image of a randomly selected string. I think this code is self explanatory. If you have any queries please post it as a comment I will reply.Below are some of the images generated by this code

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Drawing.Text
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
''' <summary>
''' This function will create a bitmap from the given string and implement random changes on that bitmap to make it a capcha.
''' </summary>
''' <param name="sImageText"></param>
''' <returns>bitmap image</returns>
''' <remarks></remarks>
Private Function CreateImage(ByVal sImageText As String)
Dim bmpImage As New Bitmap(1, 1)
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
Dim RandomInst As New Random
Dim iMinNoice As Integer = RandomInst.Next(6, 9)
Dim iMaxNoice As Integer = RandomInst.Next(9, 11)
Dim iScrachCode As Integer = RandomInst.Next(3, 6)
Dim iVerHorRand As Integer = RandomInst.Next(1, 4)
Dim iColor As System.Drawing.Color = GetColorFromID(RandomInst.Next(1, 7))
Dim i_backColor As Color = Nothing
'Create a Font object
Dim MyFont As New Font(GetRandomFontName, 30, System.Drawing.FontStyle.Bold + _
System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point)
Dim MyGraphics As Graphics
MyGraphics = Graphics.FromImage(bmpImage)
'Resize the bitmap, this is where the bitmap is sized.
iWidth = CInt(MyGraphics.MeasureString(sImageText, MyFont).Width)
iHeight = CInt(MyGraphics.MeasureString(sImageText, MyFont).Height)
bmpImage = New Bitmap(bmpImage, New Size(iWidth, iHeight - 1))
MyGraphics = Graphics.FromImage(bmpImage)
MyGraphics.Clear(Color.White)
i_backColor = bmpImage.GetPixel(0, 0)
MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
MyGraphics.DrawString(sImageText, MyFont, New SolidBrush(iColor), 0, 0)
'Put random pixels on the image
For m As Integer = 1 To iHeight - 3
For n As Integer = 1 To iWidth - 3
If m Mod RandomInst.Next(iMinNoice, iMaxNoice) = 0 Then
bmpImage.SetPixel(n, m, iColor)
ElseIf n Mod RandomInst.Next(iMinNoice, iMaxNoice) = 0 Then
bmpImage.SetPixel(n, m, iColor)
End If
Next
Next
'draw random bars on the image
For m As Integer = 1 To iHeight - 3
For n As Integer = 1 To iWidth - 3
If m Mod 10 = 0 Then
bmpImage.SetPixel(n, m, iColor)
ElseIf n Mod 10 = 0 Then
bmpImage.SetPixel(n, m, iColor)
End If
Next
Next
'draw random graphics on the bitmap
Select Case iVerHorRand
Case 1
For m As Integer = 1 To iHeight - 3
For n As Integer = 1 To iWidth - 3
If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
m Mod (iScrachCode * iScrachCode) = 0 Then
bmpImage.SetPixel(n, m, i_backColor)
End If
Next
Next
Case 2
For m As Integer = 1 To iHeight - 3
For n As Integer = 1 To iWidth - 3
If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
n Mod (iScrachCode * iScrachCode) = 0 Then
bmpImage.SetPixel(n, m, i_backColor)
End If
Next
Next
Case 3
iScrachCode = RandomInst.Next(4, 7)
For m As Integer = 1 To iHeight - 3
For n As Integer = 1 To iWidth - 3
If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
m Mod iScrachCode = 0 AndAlso n Mod iScrachCode = 0 Then
bmpImage.SetPixel(n, m, i_backColor)
End If
Next
Next
End Select
MyGraphics.Flush()
Return bmpImage
End Function
''' <summary>
''' This function will return the name of the font as per the given ID
''' </summary>
''' <param name="a_FontID"></param>
''' <returns>Name of the font as per the given ID</returns>
''' <remarks></remarks>
Private Function GetFontNameFromID(ByVal a_FontID As Integer) As String
GetFontNameFromID = Nothing
Select Case a_FontID
Case 1 : GetFontNameFromID = "Goudy Stout"
Case 2 : GetFontNameFromID = "Copperplate Gothic Bold"
Case 3 : GetFontNameFromID = "Eras Bold ITC"
Case 3 : GetFontNameFromID = "Blue Highway Linocut"
Case 4 : GetFontNameFromID = "Rockwell Extra Bold"
Case 4 : GetFontNameFromID = "Eras Bold ITC"
Case 5 : GetFontNameFromID = "Verdana"
Case 6 : GetFontNameFromID = "Algerian"
Case 7 : GetFontNameFromID = "Bodoni MT Black"
Case 8 : GetFontNameFromID = "Cooper Black"
Case 10 : GetFontNameFromID = "Gill Sans Ultra Bold"
Case 11 : GetFontNameFromID = "Rockwell Extra Bold"
Case 12 : GetFontNameFromID = "Showcard Gothic"
Case 13 : GetFontNameFromID = "Berlin Sans FB Demi"
End Select
End Function
Private Function GetCharFromID(ByVal a_CharID As Integer) As String
GetCharFromID = " "
'This function return a charecter according to the value of the variable "a_CharID"
'Here some confucing letters like I,l and 1 are avoided
Select Case a_CharID
Case 1 To 2 : GetCharFromID = Chr(76)
Case 3 To 10 : GetCharFromID = Chr(a_CharID + 47)
Case 11 To 18 : GetCharFromID = Chr(a_CharID + 54)
Case 19 : GetCharFromID = Chr(a_CharID + 53)
Case 20 To 36 : GetCharFromID = Chr(a_CharID + 54)
Case 37 To 47 : GetCharFromID = Chr(a_CharID + 60)
Case 48 : GetCharFromID = Chr(a_CharID + 59)
Case 49 To 63 : GetCharFromID = Chr(a_CharID + 60)
End Select
End Function
''' <summary>
''' Return a random color as per the color ID
''' </summary>
''' <param name="a_ColorId"></param>
Private Function GetColorFromID(ByVal a_ColorId As Integer) As Color
GetColorFromID = Color.Green
Select Case a_ColorId
Case 1 : GetColorFromID = Color.Green
Case 2 : GetColorFromID = Color.Blue
Case 3 : GetColorFromID = Color.Red
Case 4 : GetColorFromID = Color.Violet
Case 5 : GetColorFromID = Color.Black
Case 6 : GetColorFromID = Color.RosyBrown
Case 7 : GetColorFromID = Color.Brown
Case 8 : GetColorFromID = Color.Navy
Case 8 : GetColorFromID = Color.Plum
End Select
End Function
''' <summary>
''' This function will generate a random string with length of a_Length
''' </summary>
''' <param name="a_Length"></param>
Private Function GetRandomString(ByVal a_Length As String) As String
GetRandomString = ""
Dim RandomInst As New Random
'GetCharFromID() function will returns 63 different charecters according
'to the ID passing to the function. Here we are calling the function 5 times
'with a random number as ID and thus creating a charecter string with 5
'random charecters.
For i As Integer = 1 To a_Length
GetRandomString = GetRandomString + GetCharFromID(RandomInst.Next(1, 63)) '+ " "
Next
End Function
Private Function GetRandomFontName() As String
GetRandomFontName = ""
Dim RandomInst As New Random
GetRandomFontName = GetFontNameFromID(RandomInst.Next(1, 13))
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.ContentType = "image/gif"
Dim bmpImage As New Bitmap(1, 1)
Dim strImage As String = GetRandomString(5) 'Create a random string with the length of 5 charecter
'Call the CreateImage() function to save the random string to a stream which will be
'drawn in the CreateImage() function.
bmpImage = CreateImage(strImage)
Dim a As String = ""
bmpImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
Response.End()
End Sub
End Class
Related posts
aby ASP.net
Here is a simple VB.Net code to extract email addresses from a string
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub btnExtract_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtract.Click
Dim textstring As Array
Dim strEmail As String
Dim strEmailChars As String
Dim StrOutPut As String = ""
strEmail = txtMailContent.Text
StrOutPut = txtResult.Text
strEmailChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@."
For cnt As Long = 0 To strEmail.Length - 1 'Loop throug each charecters in the content
'Check whether the charecter is anything other than valid email charecters (Charecters in the strins "strEmailChars")
If strEmailChars.Contains(strEmail(cnt)) = False Then
strEmail = strEmail.Replace(strEmail(cnt), " ") 'Replace that String with space
End If
Next
textstring = Split(strEmail) ' no delimeter means use space
Dim emailpattern As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Dim i As Integer
Dim r As New Regex(emailpattern, RegexOptions.IgnoreCase + RegexOptions.Multiline)
For i = 0 To textstring.GetLength(0) - 1
Dim m As Match = r.Match(textstring(i))
While m.Success
If StrOutPut.Contains(m.ToString) = False Then
If StrOutPut.Length > 0 Then 'Put a comma before appending new email address if the
' taken email id is not the first one
StrOutPut = StrOutPut & ", "
End If
StrOutPut = StrOutPut & m.ToString()
lblCount.Text = lblCount.Text + 1
End If
m = m.NextMatch()
End While
Next i
StrOutPut = StrOutPut.Remove(StrOutPut.LastIndexOf(",") + 1, 0)
txtResult.Text = StrOutPut
MsgBox("Completed")
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblCount.Text = 0
End Sub
End Class
Now here is a detailed explanation.
Create a VB.Net 2005 application. Design a form with two text boxes (txtMailContent, txtResult), a label box (lblCount) and a button (btnExtract). Take the code window of the form and delete all the content and paste the above mentioned code.
Declare variables
Imports System.Text.RegularExpressions
‘Import name space for regular expressions.
Dim textstring As Array
Dim strEmail As String
Dim strEmailChars As String
Dim StrOutPut As String = ""
Assign string to extract to the variable strEmail and assign the current result to StrOutPut. This code will extract unique email address from the supplied string.
strEmail = txtMailContent.Text
StrOutPut = txtResult.Text
Assign legal characters to a string.
strEmailChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@."
The below loop will replace all illegal characters with an “Space”
For cnt As Long = 0 To strEmail.Length - 1 'Loop throug each charecters in the content
'Check whether the charecter is anything other than valid email charecters
'(Charecters in the strins "strEmailChars")
If strEmailChars.Contains(strEmail(cnt)) = False Then
strEmail = strEmail.Replace(strEmail(cnt), " ") 'Replace that String with space
End If
Next
Split each word and store it in an array.
textstring = Split(strEmail) ' no delimeter means use space
The belowloop will extract each word in the array and check if that is a valid email. If that is a valid email then it will check whether its a duplicate. If not then it will add that email address to result. The label lblCount will display the total number of emails extracted.
Dim emailpattern As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Dim r As New Regex(emailpattern, RegexOptions.IgnoreCase + RegexOptions.Multiline)
For i = 0 To textstring.GetLength(0) - 1
Dim m As Match = r.Match(textstring(i))
While m.Success
If StrOutPut.Contains(m.ToString) = False Then
If StrOutPut.Length > 0 Then 'Put a comma before appending new email address if the
' taken email id is not the first one
StrOutPut = StrOutPut & ", "
End If
StrOutPut = StrOutPut & m.ToString()
lblCount.Text = lblCount.Text + 1
End If
m = m.NextMatch()
End While
Next i
StrOutPut = StrOutPut.Remove(StrOutPut.LastIndexOf(",") + 1, 0)
txtResult.Text = StrOutPut
Now Display the result in to the text box txtResult.
Related posts
aby vb.net
Recent Comments