This article is based on a pre-release version of Microsoft Visual Studio 2005, formerly code-named "Whidbey." All information contained herein is subject to change.
This article discusses:
-
File System Management and its uses
-
File Permissions
-
File modes, File Access, File Shares
File Management in Whidbey/VB.NET the beating heart of the technologies that comprise Windows Forms. This article explains various file management operations such as read or write, retrieving properties or setting the properties or copy or move a file using the Whidbey/Visual Studio.NET environment. I hope this will helpful to understand the operation of managing a file.
System.IO.namespace
The input/output operation of file management is handled by the classes in the System.IO.namespace. The System.IO.namespace supports the following activities:
-
Reading and writing to binary files, text files etc.
-
Reading and writing to data streams
-
Basic file support
-
Directory support
-
Memory streams
-
Network streams
You have to include this namespace to your project.
Classes that supports System.IO.namespace
The following classes will support System.IO.namespace in VB.NET.
1. The class Binary Reader used for Reads primitive data types as binary values. Primitive data types are those that are not defined in terms of other data types. Because primitive data types are the basis for all other types, they cannot have element content or attributes. Examples of primitive data types are string, float, decimal, anyURI, and QName.
Usage:
Dim objBinReader as New BinaryReader(File.Open _ ("C:\mysamplefile.txt", FileMode.Open))
2. The class BinaryWriter used for writes primitive data types as binary values to a stream.
Usage:
Dim objBinWriter as New BinaryWriter(File.Open _ ("C:\mysamplefile.txt", FileMode.Create))
3. The class BufferedStream used for buffering to another stream for read and write operations.
Usage:
' Create an NetworkStream object "objNetStream" that owns clientSocket
Dim objNetStream As New NetworkStream(ClientSocket, True)
' Create an BufferedStream object
' "objBufStream" on top of the NetworkStream
Dim objBufStream as New _ BufferedStream(objNetStream, _ intStreamBufferSize)
Note: The NetworkStream class provides methods for sending and receiving data over Stream sockets in blocking mode
4. The class Directory used for creating, copying, moving, deleting and renaming directories and sub directories. The Directory class includes the System.IO namespace.
Some of the public methods used for Directory class is described as follows.
-
CreateDirectory - Creates a new directory in a specified path.
-
Delete - Delete the directory from the specified path
-
Exists - Checks whether the associated directories exists in the specific path.
-
GetCreationTime - Get the creation time of directory
-
GetCreationTimeUtc - Get the creation time in universal coordinated time
-
GetCurrentDirectory - Get the current working directory of the application
-
GetDirectories - Gets the name of subdirectories in the working folder
-
GetDirectoryRoot - Get root and volume information for the specified path
-
GetFiles - Get or returns all the files listed in the specified directory.
-
GetFileSytemEntries - Get or returns all the files and sub directories listed in the specified directory.
-
GetLastAccessTime - Last access date and time of the specified directory
-
GetLastAccessTimeUtc - Last access date and time of the specified directory in universal coordinator time (UTC) format.
-
GetLastWriteTime - Last written date and time of a specified directory.
-
GetLastWriteTimeUtc - Last written date and time of a specified directory in universal coordinator time (UTC).
-
GetLogicalDrives - Get all the logical drives on the computer.
-
GetParent - Get the absolute and relative path of the parent directory.
-
Move - Move the specific directory to another location
-
SetCreationTime - Sets the creation date and time for the specified directory or file.
-
SetCreationTimeUtc - Sets the creation date and time for the specified directory or file in universal coordinated time format.
-
SetCurrentDirectory - Sets the current directory to a specified directory in an application path.
-
SetLastAccessTime - Sets the last access date and time for the specified directory or file.
Usage:
' Checking whether the directory exists in the specified path
Dim strMyPath As String = "C:\mySample"
If Directory.Exists(strMyPath) = True Then
Directory.Delete(strMyPath)
Else
Directory.CreateDirectory(strMyPath)
End If
5. The class DirectoryInfo used the same functionality as Directory class except if you are not going to reuse an object several times.
6. The class DirectoryNotFoundException throws exception error when the file or directory not found in the
specified path.
7. The class EndOfStreamException throws exception error at the past the end of a stream.
8. The class ErrorEventArgs provides data for Error event.
Usage:
' Checking whether the directory exists in the specified path
Dim strMyPath As String = "C:\mySample"
' Initializes the "objException" Exception object
Dim objException As New Exception("Cannot create Directory")
' Creates an ErrorEventArgs with the exception.
Dim objErrorEventArgs As New ErrorEventArgs(objException)
If Directory.Exists(strMyPath) = True Then
Directory.Delete(strMyPath)
Else
Directory.CreateDirectory(strMyPath)
End If
' Retrieves Exception from ErrorEventArgs
Dim objReturnedException As Exception = objErrorEventArgs.GetException()
If objReturnedException.Message <> "" Then
MessageBox.Show(objReturnedException.Message
End If
End Sub
9. The class File provides to manage the file manipulation operations such as Creating, Opening, Deleting, Copying and Moving of files using FileStream objects. File class methods are static.
Some of the Public Methods used with File class is as follows:
-
AppendText
-
Copy
-
Create
-
CreateText
-
Delete
-
Exists
-
GetAttributes
-
GetCreationTime
-
GetCreationTimeUtc
-
GetLastAccessTime
-
GetLastAccessTimeUtc
-
GetLastWriteTime
-
Move
-
Open
-
OpenRead
-
OpenText
-
OpenWrite
-
SetAttributes
-
SetCreationTime
-
SetCreationTimeUtc
-
SetLastAccessTime
-
SetLastAccessTimeUtc
-
SetLastWriteTime
-
SetLastWriteTimeUtc
Usage:
' Initializing File class aids in the creation of FileStream objects
Try
Dim strMyPath As String = "C:\Benoy\temp.txt"
Dim strMyCopyPath As String = _"C:\Benoy\temp1.txt"
Dim objFS As FileStream = _ File.Create(strMyPath)
' Closing the file
objFS.Close()
' Copy the file
File.Copy(strMyPath,strMyCopyPath
' Delete the file
File.Delete(strMyPath)
Catch
MessageBox.Show("Error in File Operation")
End Try
10. The class FileInfo provides the same file management operation as in File class such as Creating, Opening, Deleting, Copying and Moving of files using FileStream objects, except if you are not reusing the object several times because the security check is not always necessary.
11. The class FileLoadException throws exception error if any error occurs during File Load.
Some of the public methods used with this class are shown below:
-
FileName - The associated filename that causes exception.
-
FusionLog - Get the log file.
-
HelpLink - Help Link association during exception error.
-
InnerException - An inner exception that caused the current exception.
-
Message - Exception Error Message.
-
Source - Error Source.
-
StackTrace - The associated string representation of the frames on the call stack.
-
TargetSite - The associated method name that causes exception.

Join the conversation! Your thoughts help the community grow.