Home

The Files of a File-Based Application

 

Introduction

As its name indicates, a file-base application uses one or more files to hold its information. If you decide to create the application using the C# language, you can take advantage of the .NET Framework rich library and its support for file processing.

In the .NET Framework, file processing is primarily supported through the System.IO namespace that is filled with various classes to deal with files and directories (folders). The most fundamental class of the System.IO namespace and used to perform file processing is called File. The abstract and sealed File class contains all necessary methods used to create a file, check the existence of a file, write information to a file, read information from a file, or manipulate the system attributes of a file.

Another one of the fundamental file processing classes is called Stream. This is mainly an abstract class that lays a foundation for other stream-oriented classes. One of the classes that derives from Stream is called FileStream.

Creating a File

To create a new file, you can use the File class, call one of the versions of its Create() method that takes an argument as the name of, or the path to, the file and returns a FileStream object.

Besides File, you can use the StreamWriter class to create a file. To do this, declare a variable of type StreamWriter and initialize it using one of its constructors.

Writing to a File

One of the most routine operations performed on a class consists of writing information to it. And one of the most useful classes in this domain is called StreamWriter. The StreamWriter class is derived from the TextWriter class. To create a file using the StreamWriter class, you can declare a StreamWriter variable and initialize it using one of its constructors. After creating the file, you can write information to it by calling the Write() or the WriteLine() method. Always make sure you close the stream after using it. Also make sure you use exception handling in your code.

Here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinForms
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StreamWriter stmWrite = new StreamWriter(txtSave.Text);
            stmWrite.WriteLine(txtFirstName.Text);
            stmWrite.WriteLine(txtLastName.Text);
            stmWrite.WriteLine(dtpDateHired.Value);
            stmWrite.WriteLine(cbxGenders.Text);
            stmWrite.WriteLine(txtHourlySalary.Text);

            stmWrite.Close();

            txtFirstName.Text = "";
            txtLastName.Text = "";
            dtpDateHired.Value = DateTime.Today;
            cbxGenders.Text = "Unknown";
            txtHourlySalary.Text = "0.00";
            txtSave.Text = "";
            txtOpen.Text = "";
        }

        private void txtLastName_Leave(object sender, EventArgs e)
        {
            string strInitials = txtFirstName.Text.Substring(0, 1) +
                                 txtLastName.Text.Substring(0, 1);
            txtSave.Text = strInitials;
        }
    }
}

Besides StreamWriter, to create a file and write information to it, you can use the BinaryWriter class. You start by declaring a BinaryWriter variable and initialize it using one of its constructors, passing a Stream-based object.

Reading From a File

Before exploring the contents of a file, you must first open. To open a file using the File class, you can call its Open method that is overloaded with three versions. If the information in the file is raw text, you can call the OpenText() method. After opening a file, you can read its content.

To support the ability to read from a file, you can use the StreamReader class. The StreamReader class is derived from the TextReader class. When using it, you can start by opening the file. To do this, declare a variable of type StreamReader and use one of its constructor to specify the name of, or the path to, the file. To read information from the file, you can call its Read() or its ReadLine() method. Here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinForms
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StreamWriter stmWrite = new StreamWriter(txtSave.Text);

            stmWrite.WriteLine(txtFirstName.Text);
            stmWrite.WriteLine(txtLastName.Text);
            stmWrite.WriteLine(dtpDateHired.Value);
            stmWrite.WriteLine(cbxGenders.Text);
            stmWrite.WriteLine(txtHourlySalary.Text);

            stmWrite.Close();

            txtFirstName.Text = "";
            txtLastName.Text = "";
            dtpDateHired.Value = DateTime.Today;
            cbxGenders.Text = "Unknown";
            txtHourlySalary.Text = "0.00";
            txtSave.Text = "";
            txtOpen.Text = "";
        }

        private void txtLastName_Leave(object sender, EventArgs e)
        {
            string strInitials = txtFirstName.Text.Substring(0, 1) +
                                 txtLastName.Text.Substring(0, 1);
            txtSave.Text = strInitials;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            StreamReader stmReader = new StreamReader(txtOpen.Text);

            txtFirstName.Text = stmReader.ReadLine();
            txtLastName.Text = stmReader.ReadLine();
            dtpDateHired.Value = DateTime.Parse(stmReader.ReadLine());
            cbxGenders.Text = stmReader.ReadLine();
            txtHourlySalary.Text = stmReader.ReadLine();

            stmReader.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Instead of StreamReader, you can use the BinaryReader class to read information from a file.

Saving and Opening Data

We have mentioned that, on a typical database, the user is not aware of opening or saving files. In the same way, the user can be spared with deciding when to save and when not to save data. Whenever possible, most operations should be performed behind-the-scenes with little to no intervention from the user. To make this possible, you are in charge of creating the file(s), receiving data from the user, and then adding that data to the file.

Data Input and Output

 

Introduction

Data input, also referred to as data entry, consists of entering the values into the application. The user does it mainly using the keyboard and the mouse. As we reviewed the Windows controls, there are various types of objects you can use to assist the user. One of the suggestions you should follow is that you should make the user's job as easy as you can. Because users of a database are not expected to do any heavy word processing. This means that typing speed is not among their strongest points. Instead, when choosing the Windows controls for your application, you should select the most appropriate one for a particular piece of information.

Printing

Data output consists of retrieving information from a database. Besides opening the objects, such as the forms, that hold the information of a database, users also regular want to print. In fact, in some businesses, the customers require to have a printed copy of their transaction. Therefore, if you are in the habit of neglecting to configuring printing in your Windows applications, for a database, you should (strongly) loose the habit and provide your users with the ability to print the data of your application.

If you are creating a file-based application, you can use the various printing classes of the .NET Framework. Unfortunately, there is no environment inside the Microsoft Visual Studio 2005 that can assist you to visually design a report. You must manually draw everything.

 

Previous Copyright © 2007 FunctionX, Inc. Home