Isaiah's Blog

A vbCity Leader's journal

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

DecJanuary 2005Feb
SMTWTFS
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

Articles

Archives

Topics

Image Galleries

Blogs I Read

Monday, January 10, 2005 #

Throughout previous versions of Visual Basic accomplishing the most simple task took several lines of code. My favorite example of how the My namespace has made life easier is with downloading files. In previous versions of VB.Net the developer had import the System.Net and several other namespaces. The following code is how one might obtain a text file from the internet without using the My namespace

Imports System.Net
Imports System.Net.Sockets

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

        Cursor = Cursors.WaitCursor

        '-- Create a new Socket for TCP/IP use
        ClientSocket = New Socket(AddressFamily.InterNetwork, _
           SocketType.Stream, ProtocolType.Tcp)
        '-- Create an endpoint to a server on port 80
        Dim EP As New _
        IPEndPoint(IPAddress.Parse("127.0.0.1"), 80) '127.0.0.1 is localhost
        '-- Connect to the Endpoint
        ClientSocket.Connect(EP)
        '-- Send an HTTP string to retrieve a document
        Dim SendThis() As Byte = _
          System.Text.ASCIIEncoding.ASCII.GetBytes("GET /sockets1.txt" & _
          vbCrLf) 'Change the file name
        ClientSocket.Send(SendThis)
        '-- Wait for received data
        Do
            '-- Is data available?
            If ClientSocket.Poll(1, SelectMode.SelectRead) = True Then
                '-- Available property returns number of bytes ready
                ' to be read. You must read into a byte array.
                ' This is because a character can be more than
                ' one byte if Unicode.
                Dim ReceiveThis(ClientSocket.Available) As Byte
                '-- Read the data
                ClientSocket.Receive(ReceiveThis)
                '-- Convert the byte array to an ASCII string
                TextBox1.Text = _
                  System.Text.ASCIIEncoding.ASCII.GetString(ReceiveThis)
                '-- All done!
                Exit Do
            Else
                '-- Allow other processes to occur
                Application.DoEvents()
                '-- Put the PC to sleep for 1 millisecond to avoid
                ' CPU overload
                System.Threading.Thread.Sleep(1)
            End If
        Loop
        '-- Shutdown and Close the socket. You must always do this
        ClientSocket.Shutdown(SocketShutdown.Both)
        ClientSocket.Close()

        Cursor = Cursors.Default
    End Sub

Roughly twenty-five lines, of actually, code can be condensed to one line. Please note that the code above only displays the text received in a text box. Whereas, the code below will actually download the file to your physical disk.

My.Computer.Network.DownloadFile(”http://127.0.0.1/VBRocks.txt”, “C:\My Documents\VBRocks.txt”)

My is one of several new features that we have to look forward to. What is your favorite feature of My?

posted @ 9:33 PM