1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Check for Files and Directories with VB.Net

Discussion in 'Visual Basic [VB]' started by pradeep, May 4, 2007.

  1. pradeep

    pradeep Team Leader

    An important function of any program is the ability to work with a file system and manipulate files and directories. In this tip, we examine how to work with a file system using VB.NET. Lets see how to perform necessary functions with examples that utilise the existing classes, their methods, and widely used properties.

    In order to work with a file system, we need to use the System.IO namespace; therefore, we would have a line imports System.IO at the top of the form or the module. We'll focus our attention on the FileInfo class, which allows you to access and manipulate files on the filesystem. It also lets you determine the files' properties and DirectoryInfo, a class that allows you to access and manipulate directories and determine various properties.

    Determining if a file exists



    The following example shows how to determine whether a file exists and how to check one of its properties. In the example, we define the variable sFileName to hold the name and location of the file on the file system. Then we create an instance of a FileInfo class, which accepts the full file path as a parameter. Next, we utilise its Exists property to determine if a file exists. If there is a file, we check and display the date/time of the file creation; if there is not a file, we display a message stating that it cannot be located.

    Code:
    Private Function DetermineIfFileExists()  As Integer
      Dim sFileName As String
      sFileName = "C:/text1.txt"
      Dim fFile As New FileInfo(sFileName)
      
      
      If Not fFile.Exists Then
          MessageBox.Show("File Not Found")
      Else
          MessageBox.Show("File Found. File was created on: " & fFile.CreationTime)
      End If
    End Function

    Determining if a directory exists



    The next example shows how to determine whether a directory exists and how to check one of its properties. We define the variable sDirName to hold the name and location of the Directory on the file system. Then we create an instance of a DirectoryInfo class, which accepts the full directory name as a parameter. Next, we utilize its Exists property to determine if a directory exists. If there is a directory, we check and display the date/time when it was last accessed; if there is not a directory, we display a message stating that it cannot be located.

    Code:
    Private Function DetermineIfDirectoryExists() As Integer
      Dim sDirName As String
      sDirName = "C:/temp"
      Dim dDir As New DirectoryInfo(sDirName)
      
      
      If Not dDir.Exists Then
          MessageBox.Show("Directory Not Found")
      Else
          MessageBox.Show("Directory Found. Directory was last accessed on: " & dDir.LastAccessTime)
      End If
      
    End Function
     

Share This Page