RE: Uploading a Picture

From: Santhosh Kutty (sakutty_at_online.microsoft.com)
Date: 06/02/04


Date: Wed, 02 Jun 2004 02:38:58 GMT

Hi, I found this article after a bit of researching. I've tested all the
modules. Please revert if you face any issues.

This shows you how to do six basic file input/output (I/O) operations in
Visual Basic .NET. You will find that the object model for file operations
in .NET is similar to the FileSystemObject (FSO) that is popular with many
Visual Studio 6.0 developers. To make the transition easier, the
functionality that is demonstrated in this article is based on the
following Microsoft Knowledge Base article:
186118 HOWTO: Use FileSystemObject with Visual Basic

I hope this article helps.

You can still use the FileSystemObject in .NET. Because the
FileSystemObject is a Component Object Model (COM) component, .NET requires
that access to the object be through the Interop layer. .NET generates a
wrapper for the component for you if you want to use it. However, the File,
FileInfo, Directory, DirectoryInfo classes, and other related classes in
the .NET Framework, offer functionality that is not available with the FSO,
without the overhead of the Interop layer.

Demonstrated File I/O Operations
The examples in this article describe basic file I/O operations. The
"Step-by-Step Example" section describes how to create a sample program
that demonstrates the following six file I/O operations:

Read a Text File
Write a Text File
View File Information
List Disk Drives
List Folders
List Files

Read a Text File
The following sample code uses a StreamReader class to read the System.ini
file. The contents of the file are added to a ListBox control. The
try...catch block is used to alert the program if the file is empty. There
are many ways to determine when the end of the file is reached; this sample
uses the Peek method to examine the next line before reading it. Dim
reader As StreamReader = _
    New StreamReader(winDir & "\system.ini")
    Try
        Me.ListBox1.Items.Clear()
        Do
            Me.ListBox1.Items.Add(reader.ReadLine)
        Loop Until reader.Peek = -1

    Catch
        Me.ListBox1.Items.Add("File is empty")

    Finally
        reader.Close()
    End Try
                                
 
Write a Text File
This sample code uses a StreamWriter class to create and write to a file.
If you have an existing file, you can open it in the same way. Dim
writer As StreamWriter = _
    New StreamWriter("c:\KBTest.txt")
    writer.WriteLine("File created using StreamWriter class.")
    writer.Close()
                                
 
View File Information
This sample code uses a FileInfo object to access a file's properties.
Notepad.exe is used in this example. The properties appear in a ListBox
control. Dim FileProps As FileInfo = New FileInfo(winDir &
"\notepad.exe")
    With Me.ListBox1.Items
        .Clear()
        .Add("File Name = " & FileProps.FullName)
        .Add("Creation Time = " & FileProps.CreationTime)
        .Add("Last Access Time = " & FileProps.LastAccessTime)
        .Add("Last Write Time = " & FileProps.LastWriteTime)
        .Add("Size = " & FileProps.Length)
    End With
    FileProps = Nothing
                                
 
List Disk Drives
This sample code uses the Directory and Drive classes to list the logical
drives on a system. For this sample, the results appear in a ListBox
control. Dim dirInfo As Directory
    Dim drive As String
    Me.ListBox1.Items.Clear()
    Dim drives() As String = dirInfo.GetLogicalDrives()
    For Each drive In drives
        Me.ListBox1.Items.Add(drive)
    Next
                                
 
List Subfolders
This sample code uses the GetDirectories method of the Directory class to
get a list of folders. Dim dir As String
    Me.ListBox1.Items.Clear()
    Dim dirs() As String = Directory.GetDirectories(winDir)
    For Each dir In dirs
        Me.ListBox1.Items.Add(dir)
    Next
                                
 
List Files
This sample code uses the GetFiles method of the Directory class to get a
list of files. Dim file As String
    Me.ListBox1.Items.Clear()
    Dim files() As String = Directory.GetFiles(winDir)
    For Each file In files
        Me.ListBox1.Items.Add(file)
    Next
                                
Many things can go wrong when a user gains access to files. The files may
not exist, the files may be in use, or users may not have rights on the
files of folders that they are trying to access. It is important to
consider these possibilities when you write code and to handle the
exceptions that may be generated.

 
Step-by-Step Example
In Visual Basic .NET, create a new Windows Application. By default, Form1
is created.
Open the code window for Form1.
Delete all of the code in the Code-Behind Editor.
Paste the following sample code into the Code-Behind Editor window.Option
Strict On
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private winDir As String
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'Windows Form Designer requires this call.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.

    End Sub

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
                        Friend WithEvents ListBox1 As
System.Windows.Forms.ListBox
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    
    'Windows Form Designer requires this call.
    Private components As System.ComponentModel.Container

    'NOTE: Windows Form Designer requires the following procedure.
    'You can use the Windows Form Designer to modify it; however, do not
    'use the Code Editor to modify it.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button6 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(272, 64)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(136, 23)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(272, 96)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(136, 23)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "Button3"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16, 16)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(240, 238)
        Me.ListBox1.TabIndex = 5
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(272, 128)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(136, 23)
        Me.Button4.TabIndex = 3
        Me.Button4.Text = "Button4"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(272, 160)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(136, 23)
        Me.Button5.TabIndex = 4
        Me.Button5.Text = "Button5"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(272, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(136, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(272, 192)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(136, 23)
        Me.Button6.TabIndex = 6
        Me.Button6.Text = "Button6"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 341)
        Me.Controls.AddRange(New System.Windows.Forms.Control() _
           {Me.Button6, Me.ListBox1, Me.Button5, Me.Button4, _
           Me.Button3, Me.Button2, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) _
      Handles MyBase.Load
        Me.Button1.Text = "Read Text File"
        Me.Button2.Text = "Write Text File"
        Me.Button3.Text = "View File Information"
        Me.Button4.Text = "List Drives"
        Me.Button5.Text = "List Subfolders"
        Me.Button6.Text = "List Files"
        winDir = System.Environment.GetEnvironmentVariable("windir")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
      Handles Button1.Click
        'Demonstrates how to read a file by using StreamReader
        'Uses System.ini as an example
        'try...catch is used to detect a 0 byte file.
        Dim reader As StreamReader = _
            New StreamReader(winDir & "\system.ini")
        Try
            Me.ListBox1.Items.Clear()
            Do 'Until reader.Peek = -1
                Me.ListBox1.Items.Add(reader.ReadLine)
            Loop Until reader.Peek = -1

        Catch
            Me.ListBox1.Items.Add("File is empty")

        Finally
            reader.Close()
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
      Handles Button4.Click
        'Demonstrates how to obtain a list of disk drives
        Dim dirInfo As Directory
        Dim drive As String
        Me.ListBox1.Items.Clear()
        Dim drives() As String = dirInfo.GetLogicalDrives()
        For Each drive In drives
            Me.ListBox1.Items.Add(drive)
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
      Handles Button3.Click
        'Demonstrates how to access file properties. You can access folder
properties
        'in the same way. More properties are available through the
FileInfo class
        'than are demonstrated here.
        'You can also use the Directory class to obtain this information.
        Dim FileProps As FileInfo = New FileInfo(winDir & "\notepad.exe")
        With Me.ListBox1.Items
            .Clear()
            .Add("File Name = " & FileProps.FullName)
            .Add("Creation Time = " & FileProps.CreationTime)
            .Add("Last Access Time = " & FileProps.LastAccessTime)
            .Add("Last Write TIme = " & FileProps.LastWriteTime)
            .Add("Size = " & FileProps.Length)
        End With
        FileProps = Nothing
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
      Handles Button2.Click
        'Demonstrates how to create and write to a text file
        Dim writer As StreamWriter = _
            New StreamWriter("c:\KBTest.txt")
        writer.WriteLine("File created using StreamWriter class.")
        writer.Close()
        Me.ListBox1.Items.Clear()
        Me.ListBox1.Items.Add("File Written to C:\KBTest.txt")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
      Handles Button5.Click
        'Demonstrates how to get a list of folders (example uses Windows
folder)
        Dim dir As String
        Me.ListBox1.Items.Clear()
        Dim dirs() As String = Directory.GetDirectories(winDir)
        For Each dir In dirs
            Me.ListBox1.Items.Add(dir)
        Next
    End Sub

    Private Sub Button6_Click(ByVal sender As Object, ByVal e As
System.EventArgs) _
      Handles Button6.Click
        'Demonstrates how to get a list of files (example uses Windows
folder)
        Dim file As String
        Me.ListBox1.Items.Clear()
        Dim files() As String = Directory.GetFiles(winDir)
        For Each file In files
            Me.ListBox1.Items.Add(file)
        Next
    End Sub
End Class
                                        
Press F5 to build and then run the program. Click the buttons to view the
different actions. When you view the sample code, you may want to collapse
the area named Windows Form Designer Generated Code to hide this code.

Santhosh James Kutty

Microsoft Partner Support Engineer

This posting is provided “AS IS” with no warranties, and confers no rights.
 

Get Secure! - www.microsoft.com/security