File System Control In Visual Basic

File System Control In Visual Basic Average ratng: 4,1/5 6212 reviews
-->

System Objects Chapter 22 A Survey of Database Development Using Visual Basic 2. Built-in VB File-Processing System Controls. Drive list box - available disk. Many applications must present information about disk drives, directories, and files. To allow users of your applications to explore the file system, Visual Basic.

The My.Computer.FileSystem object provides tools for working with files and folders. Its properties, methods, and events allow you to create, copy, move, investigate, and delete files and folders. My.Computer.FileSystem provides better performance than the legacy functions (FileOpen, FileClose, Input, InputString, LineInput, etc.) that are provided by Visual Basic for backward compatibility.

In This Section

Reading from Files
Lists topics dealing with using the My.Computer.FileSystem object to read from files

Writing to Files
Lists topics dealing with using the My.Computer.FileSystem object to write to files

Creating, Deleting, and Moving Files and Directories
Lists topics dealing with using the My.Computer.FileSystem object to creating, copying, deleting and moving files and folders.

Parsing Text Files with the TextFieldParser Object
Discusses how to use the TextFieldReader to parse text files such as logs.

File Encodings
Describes file encodings and their use.

Walkthrough: Manipulating Files and Directories in Visual Basic
Demonstrates how to create a utility that reports information about files and folders.

Troubleshooting: Reading from and Writing to Text Files
Lists common problems encountered when reading and writing to text files, and suggests remedies for each.

  • VB.Net Basic Tutorial
  • VB.Net Advanced Tutorial
  • VB.Net Useful Resources
  • Selected Reading

VB.Net allows you to work with the directories and files using various directory and file-related classes like, the DirectoryInfo class and the FileInfo class.

The DirectoryInfo Class

The DirectoryInfo class is derived from the FileSystemInfo class. It has various methods for creating, moving, and browsing through directories and subdirectories. This class cannot be inherited.

Following are some commonly used properties of the DirectoryInfo class −

Sr.No.Property Name & Description
1

Attributes

Gets the attributes for the current file or directory.

2

CreationTime

Gets the creation time of the current file or directory.

3

Exists

Gets a Boolean value indicating whether the directory exists.

4

Extension

Gets the string representing the file extension.

5

FullName

Gets the full path of the directory or file.

6

LastAccessTime

Gets the time the current file or directory was last accessed.

7

Name

Gets the name of this DirectoryInfo instance.

Following are some commonly used methods of the DirectoryInfo class −

Sr.No.Method Name & Purpose
1

Public Sub Create

Creates a directory.

2

Public Function CreateSubdirectory (path As String ) As DirectoryInfo

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

3

Public Overrides Sub Delete

Stardraw lighting 2d serialization. Deletes this DirectoryInfo if it is empty.

4

Public Function GetDirectories As DirectoryInfo()

Returns the subdirectories of the current directory.

5

Public Function GetFiles As FileInfo()

Returns a file list from the current directory.

For complete list of properties and methods please visit Microsoft's documentation.

The FileInfo Class

The FileInfo class is derived from the FileSystemInfo class. It has properties and instance methods for creating, copying, deleting, moving, and opening of files, and helps in the creation of FileStream objects. This class cannot be inherited.

Following are some commonly used properties of the FileInfo class −

Sr.No.Property Name & Description
1

Attributes

Gets the attributes for the current file.

2

CreationTime

Gets the creation time of the current file. Suzuki gs150r repair manual download.

3

Directory

Gets an instance of the directory, which the file belongs to.

4

Exists

Gets a Boolean value indicating whether the file exists.

5

Extension

Gets the string representing the file extension.

6

FullName

Gets the full path of the file.

7

LastAccessTime

Gets the time the current file was last accessed.

8

LastWriteTime

Gets the time of the last written activity of the file.

9

Length

Gets the size, in bytes, of the current file.

10

Name

Gets the name of the file.

Following are some commonly used methods of the FileInfo class −

Sr.No.Method Name & Purpose
1

Public Function AppendText As StreamWriter

Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

2

Public Function Create As FileStream

Creates a file.

3

Public Overrides Sub Delete

Deletes a file permanently.

4

Public Sub MoveTo (destFileName As String )

Moves a specified file to a new location, providing the option to specify a new file name.

5

Public Function Open (mode As FileMode) As FileStream

Opens a file in the specified mode.

6

Public Function Open (mode As FileMode, access As FileAccess ) As FileStream

Opens a file in the specified mode with read, write, or read/write access.

7

Public Function Open (mode As FileMode, access As FileAccess, share As FileShare ) As FileStream

Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.

8

Public Function OpenRead As FileStream

Creates a read-only FileStream

9

Public Function OpenWrite As FileStream

Creates a write-only FileStream.

Control

For complete list of properties and methods, please visit Microsoft's documentation

Example

The following example demonstrates the use of the above-mentioned classes −

When you compile and run the program, it displays the names of files and their size in the Windows directory.