C# File Handling Programming Examples and Practice Question

example
In this chapter you will learn:

  • C# File Handling with Programming Example
  • Practice Question and Their Answer.

This chapter is designed for learn C# File Handling with practice question and answer. Your understanding of File Handling will be enhanced more by reading this chapter.

Qu 1. Make a D:\csharp\example.txt file using following class and Read and Write Date and Time. You must make D:\csharp folder before executing this program otherwise it will throw DirectoryNotFound Exception.

  1. FileStream Class
  2. StreamWriter and StreamReader
  3. TextWriter and TextReader
  • Answer

    FileStream

    Output

    Data Added
    17-08-2016 AM 08:27:31
    _

    StreamWriter and StreamReader

    Output

    Successfully Added Current Date and Time
    Reading Current Time : 17-08-2016 AM 08:29:00
    _

    TextWriter and TextReader

    Output

    Successfully Added Current Date and Time
    Reading Current Time : 17-08-2016 AM 08:31:23
    _
    Qu 2. Make D:\csharp\example.dat file using BinaryWriter class, store current date and time and read that text using BinaryReader class.

    Answer

    Output

    Current Date and Time is : 17-08-2016 AM 08:33:45
    _
    Qu 3. Manipulate following string using StringWriter and StringReader class.

    “I am reading File Handling at Complete C# Tutorial.com”

    Answer

    Output

    I am reading
    File Handling at
    Complete C# Tutorial.com
    _
    Qu 4. Create a Directory D:\example and then create a file in it D:\example\test.txt and store “Hello File Handling” text in it. Then gather information of Directory and File and print them on console.

    Answer

    Output

    Directory Created Successfully. Information of Directory:
    Directory Name : example
    Path : D:\example
    Directory is created on : 01-01-1601 AM 05:30:00
    Directory is Last Accessed on 01-01-1601 AM 05:30:00

    ******Display File Info******
    File Create on : 17-08-2016 AM 07:42:30
    Directory Name : D:\example
    Full Name of File : D:\example\test.txt
    File is Last Accessed on : 17-08-2016 AM 07:42:30
    _

    Summary
    In this chapter we have tried to teach you File Handling using programming examples and codes. I am sure that your understanding of file handling would be increased. Next chapter have some programming exercise for you. You must resolve the question before proceeding next.


    Leave a Reply

    Your email address will not be published. Required fields are marked *