En IRC-klient skriven i VB.net och CSharp

Jag hittade precis den här kodbiten som tydligen är en simpel IRC-klient skriven i VB.net. (på sidan jag länkar till längst ner (källa) finns koden för CSharp om nån vill ha den. Det finns även ett par kommentarer som tar upp frågor om koden som kan vara bra att läsa. (Cache-version av sidan).

VB.net kod:

Namespace ircclient
Class MainClass
Public Shared Sub Main(args As String())
Dim port As Integer
Dim buf As String, nick As String, owner As String, server As String, chan As String
Dim sock As New System.Net.Sockets.TcpClient()
Dim input As System.IO.TextReader
Dim output As System.IO.TextWriter

'Get nick, owner, server, port, and channel from user
Console.Write("Enter bot nick: ")
nick = Console.ReadLine()
Console.Write("Enter bot owner name: ")
owner = Console.ReadLine()
Console.Write("Enter server name: ")
server = Console.ReadLine()
Console.Write("Enter port number: ")
port = Convert.ToInt32(Console.ReadLine())
Console.Write("Channel: ")
chan = Console.ReadLine()

'Connect to irc server and get input and output text streams from TcpClient.
sock.Connect(server, port)
If Not sock.Connected Then
Console.WriteLine("Failed to connect!")
Return
End If
input = New System.IO.StreamReader(sock.GetStream())
output = New System.IO.StreamWriter(sock.GetStream())

'Starting USER and NICK login commands
output.Write("USER " & nick & " 0 * :" & owner & vbCr & vbLf & "NICK " & nick & vbCr & vbLf)
output.Flush()

'Process each line received from irc server
buf = input.ReadLine()
While True

'Display received irc message
Console.WriteLine(buf)

'Send pong reply to any ping messages
If buf.StartsWith("PING ") Then
output.Write(buf.Replace("PING", "PONG") & vbCr & vbLf)
output.Flush()
End If
If buf(0) <> ":"C Then
Continue While
End If

' IRC commands come in one of these formats:
' * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
' * :SERVER COMAND ARGS ... :DATA\r\n
'

'After server sends 001 command, we can set mode to bot and join a channel
If buf.Split(" "C)(1) = "001" Then
output.Write("MODE " & nick & " +B" & vbCr & vbLf & "JOIN " & chan & vbCr & vbLf)
output.Flush()
End If
buf = input.ReadLine()
End While
End Sub
End Class
End Namespace

Källa: https://jakash3.wordpress.com/2012/02/13/simple-vb-net-and-csharp-irc-client/

Plocka ut filer ur katalog – VB.net

Plocka ut filer ur katalog – VB.net

Dim folderInfo As New IO.DirectoryInfo("c:\windows")
Dim arrFilesInFolder() As IO.FileInfo
Dim fileInFolder As IO.FileInfo

arrFilesInFolder = folderInfo.GetFiles("*.*")
For Each fileInFolder In arrFilesInFolder
ListBox1.Items.Add(fileInFolder.Name)
Next

VB.net MD5 – Create MD5 and Verify MD5

VB.net MD5 – Create MD5 and Verify MD5

Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Example

' Hash an input string and return the hash as
' a 32 character hexadecimal string.
Function getMd5Hash(ByVal input As String) As String
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()

' Convert the input string to a byte array and compute the hash.
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()

' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i

' Return the hexadecimal string.
Return sBuilder.ToString()

End Function

' Verify a hash against a string.
Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
' Hash the input.
Dim hashOfInput As String = getMd5Hash(input)

' Create a StringComparer an compare the hashes.
Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

If 0 = comparer.Compare(hashOfInput, hash) Then
Return True
Else
Return False
End If

End Function

Remove item from array in VB.net

Remove item from array in VB.net:
Ta bort sak från array i vb.net

'1. order is not preserved In this approach, but very simple
MyArray(ItemToBeDeleted) = MyArray( Ubound (MyArray))
Redim Preserve MyArray( Ubound (MyArray) - 1)

'2. order Is maintained
Dim i As Long , N As Long
'assumes N is the position In MyArray to remove
For i = N To Ubound (MyArray) - 1
MyArray(i) = MyArray(i + 1)
Next i
Redim Preserve MyArray( Ubound (MyArray) - 1)

Läser in hela filen i en variabel – VB.net

Läser in hela filen i en variabel – VB.net

Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = “”) As String

Dim strContents As String
Dim objReader As StreamReader
Try

objReader = New StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function

Spara enkelt ner i textfil – VB.net

Så här sparar man enkelt ner något i txt-filer i VB.net

 

 

Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = “”) As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)

objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function