Sponsored Links
VB.NET 2005 Tutorials
- VB.NET 2005 Free Training
- Shared Assembly
- The .NET Framework Architecture Part 1
- Tracing VB.NET Windows Application
- The .NET Framework Architecture Part 2
- VB.NET Windows Application Testing
- Implementing Inheritance
- The File Types Editor
- Visual Studio.NET Namespaces
- Differences between VB.NET 1.0 and VB.NET 2.0
- Visual Studio Windows Forms Designer
- Introducing VB.NET Windows Forms
- Event Handling In Visual Basic .NET
- Exploring the Forms Designer generated code
- Building Graphical Interface elements
- Microsoft .NET Creating Installation Components
- Application Class and Message Class
- Visual Studio Adding Controls to Windows Form
- Common Controls and Handling Control Events
- Implementing Class Library Object
Tutorials
VB.NET 2005Working with File System in .NET
Working with File System in .NET
In this tutorial you will learn about Access and Manipulate Data, Working with Disk Files, Browsing for Files, Streams and Backing Stores, Using the FileStream Class, Using the StreamReader and StreamWriter Classes, Using the BinaryReader and BinaryWriter Classes.
Access and Manipulate Data - Working with Disk Files
Working with Disk Files
Temporary storage of data as in the illustrations above is not the optimal solution. Persistent data storage is a file having a collection of bytes and is stored as data streams. All read write operations to these data steams can be done with ease in the circumstances. In the following illustration we shall see how read write operations can be performed on persistent data storage.
Browsing For Files
The System.IO.Directory class can be used for typical operations such as copying, moving, renaming, creating and deleting directories. This is a static class and therefore is efficient in the performance of single operations. This class can be used to perform operations relating to browsing for files, The System.IO.FileInfo.Name method of this class used to see the files in the directory. The System.IO.Directory.GetDirectories or System.DirectoryInfo.GetDirectories methods enable users see the sub directories in a the directory. Let us understand the issue by the following example.
-
Create a new windows application in the Visual Basic Express and
-
Give the name of the project as FileBrowse1.
-
To the form Form1 add two Labels, a TextBox , a CheckBox, and two Buttons.
-
Arrange the controls and name them as shown in the screenshot shown below:

Now add the necessary code to the application. The code for the application is shown below:
Click here to veiw sample code
Now press F5 to execute the application. In the window type the directory name that we want to browse and also use the check box to specify if we want to view the file names or not. Now click the button Browse to view the file names and the directory names. The outputs generated for both with and without the check box selected are shown below:


Streams
Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or TCP/IP socket. The stream class and its derived classes provide generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices.
Streams are concerned with three fundamental operations:
1.2.3.Depending on the underlying data source or repository, streams might support only some of the several capabilities. An application may query a stream for its capabilities by using the System.IO.Stream.CanRead, System.IO.Stream.CanWrite, and System.IO.Stream.CanSeek properties.
Using FileStream Class
The FileStream class gives the user the capability to read from, write to, open, and close files on a file system. This class also can be used to manipulate other file related handlers like pipes, standard input, and standard output. The user can also specify read and write operations to be either Synchronous or asynchronous.
Some of the functions ofthe FileStream class are given below:
-
Determine if a file exists.
-
The following example will illustrate some of the file operations. The two programs that are included will perform functions like reading the directory path, creating a file and listing it and also writing to a file and reading from a file.
1. Create a new Windows Application in Visual Basic Express.
2. Add a Label and three buttons and arrange them as shown in the screenshot below:

Add to the form the following code and save the project.
Click here to view sample code
Now press F5 to execute the file. The outputs generated by the program are shown below:

.

.
Next Page: Working with File System in .NET - Page 2
Read Next: ADO.NET Object Model
Comments
Kel said:
I'm wanting to append many text files together in a given directory(say kel.txt,kel1.text,kel3.txt,kel4.txt exist in a directory i.e C:Temp) How can i append them together and save resulting data in a .txt file called appendText.txt.
can anyone help..
cheersMuhammad Awais said:
This is very good article. This article helped me very much in Stringreader and stringwriter . Very nice.
Sponsored Links
