Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

Working with File System in .NET

By Exforsys | on July 11, 2005 |
VB.NET 2005

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.

  1. Create a new windows application in the Visual Basic Express and
  2. Give the name of the project as FileBrowse1.
  3. To the form Form1 add two Labels, a TextBox , a CheckBox, and two Buttons.
  4. 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. Reading. Transfer of data from the stream into a data structure, such as an array of bytes.

2. Writing to streams – Transferring data from a data structure into a stream.

3. Support to seeking – Querying and modifying of the current position within a stream.

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:

  • Create a text file.
  • Write to a text file.
  • Read from a text file.
  • Append text to a file.
  • Rename or move a file.
  • Delete a file.
  • Copy a file.
  • Get the size of a file.
  • Get the attributes of a file.
  • Set the attributes of a file.
  • Determine if a file exists.
  • Read from a binary file.
  • Write to a binary file.
  • Retrieve a file extension.
  • Retrieve the fully qualified path of a file.
  • Retrieve the file name and extension from a path.
  • Change the extension of a file.


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:

.

.
}
.
.
.


Using StreamReader and StreamWriter Classes

StreamReader is useful for reading lines of information from standard text file and StreamWriter is useful for writing to standard text files. We shall see a an example that illustrates these functions.

  1. Create a new Visual Basic Windows project.
  2. Add a Label, a RichTextBox and four Buttons.
  3. Arrange them on the form as shown in the following screenshot.


We are using the methods of FileStream, StreamReader and StreaWriter to create a file, write data to it and read from the file. The codes for the program are given below:

Click here to view sample code

The out put window and the text file the program has generated are shown below:

Opening Screen With text:

The program window after writing and reading Twice:
.

The text file created is shown below:



Using BinaryReader and BinaryWriter Classes

BinaryWriter class is used to write binary data to a file. The BinaryReader is useful to us for reading the data from the BinaryFile. Let us see an illustration of the utilities of this class.
  1. Create a new project as Windows Application.
  2. Add two Buttons, a Label and a RichTextBox t the form.
  3. Arrange the controls so that the form looks like the screenshot given below:



Now add the following code to the form.

Click here to view sample code

This program will create a binary file and also read the binary file. Now press F5 to execute the program. A form will appear. Click the button ReadWrite. The output of the program is shown below:

The bin file that was created by this action is opened with text editor and shown below:

« « SQL Server Ad Hoc Queries
SQL Server Stored Procedures » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • .NET Common Windows Forms Controls Part 2

    July 2, 2005 - 0 Comment
  • Web Services – SOAP, WSDL, Disco and UDDI

    July 16, 2005 - 0 Comment
  • Customizing Setup Project in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Exploring the Forms Designer generated code

    June 7, 2005 - 0 Comment
  • Simple Data Binding

    July 7, 2005 - 0 Comment
  • Working with Menu Controls

    July 3, 2005 - 0 Comment
  • Creating Web Service

    July 16, 2005 - 0 Comment
  • Deploying Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Application Class and Message Class

    June 8, 2005 - 0 Comment
  • .NET Complex Data Binding

    July 7, 2005 - 0 Comment
  • Microsoft .NET Creating Installation Components

    August 10, 2005 - 0 Comment
  • Shared Assembly

    August 9, 2005 - 0 Comment
  • The File Types Editor

    August 9, 2005 - 0 Comment
  • Tracing VB.NET Windows Application

    August 9, 2005 - 0 Comment
  • VB.NET Windows Application Testing

    August 9, 2005 - 0 Comment
  • The Registry Editor in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Customizing Setup Project in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Deploying Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Debugging Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Working with Legacy Code and COM Components

    July 30, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Microsoft .NET Creating Installation Components
  • Shared Assembly
  • The File Types Editor
  • Tracing VB.NET Windows Application
  • VB.NET Windows Application Testing

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT