Hur man laddar ner HTML med VB.net

Hur man laddar ner HTML med VB.net:

Imports System.Net
Imports System.IO

Module Module1

    Public Function ScreenScrapeHtml(ByVal url As String) As String

        Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()

        Return result

    End Function

End Module

Användning:
TextBox.Text = ScreenScrapeHtml(”http://www.expressen.se”)

Skriva/Läsa Textfiler – VB.net Funktion

Skriva/Läsa Textfiler – VB.net Funktion.

Skapa en modul och lägg in detta:

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

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

Imports System.IO <-- Måste också vara med.

URLencode och URLdecode funktioner för Visual Basic

URLencode och URLdecode funktioner för Visual Basic

 

Public Function UrlEncode(ByVal s As String, Optional ByVal _
use_plus_for_space As Boolean = True) As String
Dim result As String = ””

For Each ch As String In s
Select Case Asc(ch)
Case 48 To 57, 65 To 90, 97 To 122
result &= ch
Case 32
If use_plus_for_space Then
result &= ”+”
Else
result &= ”%20”
End If
Case Else
result &= ”%” & Hex(Asc(ch)).PadLeft(2, _
”0”c)
End Select
Next ch

Return result
End Function

Public Function UrlDecode(ByVal s As String, Optional ByVal _
use_plus_for_space As Boolean = True) As String
Dim result As String = ””

Dim i As Integer = 0
Do While i < s.Length
Dim ch As String = s.Substring(i, 1)

Select Case ch
Case ”+”
If use_plus_for_space Then
result &= ”+”
Else
result &= ” ”
End If
i += 1
Case ”%”
Dim num As Integer = _
Val(”&H” & s.Substring(i + 1, 2))
result &= Chr(num)
i += 3
Case Else
result &= ch
i += 1
End Select
Loop

Return result
End Function

Skriva textfil med vb.net

’Lägg till denna rad överst i ditt program

Imports System.IO ’Importerar System Imports Object för skrivning och läsning av filer


’Sedan denna rad som öppnar textfilen

Dim TextFile1 As New StreamWriter(”c:\hej.txt”)

’Dessa rader skriver och stänger filen

TextFile1.WriteLine(”Denna text hamnar i filen”)

TextFile1.Close()

Läsa in textfil med vb.net

Först så skriver vi detta längst upp av allt:
Imports System.IO

Denna rad öppnar textfilen:
Dim ioFile as new StreamReader(”TextFile.txt”)

Dessa rader loopar igenom hela filen:
Dim ioLine as string ’ Going to hold one line at a time
Dim ioLines as string ’ Going to hold whole file
ioLine = ioFile.ReadLine
ioLines = ioLine
While not ioLine = ””
ioLine = ioFile.ReadLine
ioLines = ioLines & vbcrlf & ioLine
End While

Detta skriver ut filen i en msgbox:
MsgBox(ioLines)