Home

Details on File Processing

   

Exception Handling in File Processing

 

Finally

So far, to handle exceptions, we were using the try, catch, and throw keywords. These allowed us to perform normal assignments in a try section and then handle an exception, if any, in a catch block.

 

In the previous lesson, we mentioned that, when you create a stream, the operating system must allocate resources and dedicate them to the file processing operations. Additional resources may be provided for the object that is in charge of writing to, or reading from, the stream. We also saw that, when the streaming was over, we should free the resources and give them back to the operating system. To do this, we called the Close() method of the variable that was using resources.

More than any other assignment, file processing is in prime need of exception handling. As we will see in the next section, during file processing, there are many things that can go wrong. For this reason, the creation and/or management of streams should be performed in a try block to get ready to handle exceptions that would occur. Besides actually handling exceptions, the C# language provides a special keyword used free resources. This keyword is finally.

The finally keyword is used to create a section of an exception. Like catch, a finally block cannot exist by itself. It can be created following a try section. The formula used would be:

try
{
}
finally
{
}

Based on this, the finally section has a body of its own, delimited by its curly brackets. Like catch, the finally section is created after the try section. Unlike catch, finally never has parentheses and never takes arguments. Unlike catch, the finally section is always executed. Because the finally clause always gets executed, you can include any type of code in it but it is usually appropriate to free the resources that were allocated earlier. Here is an example:

using System;
using System.IO;

public class Program
{
    static int Main(string[] args)
    {
        var Filename = "Members.clb";
        var fstPersons = new FileStream(Filename, FileMode.Create);
        var wrtPersons = new BinaryWriter(fstPersons);
        
        try 
        {
            wrtPersons.Write("James Bloch");
            wrtPersons.Write("Catherina Wallace");
            wrtPersons.Write("Bruce Lamont");
            wrtPersons.Write("Douglas Truth");
        }
        finally
        {
            wrtPersons.Close();
            fstPersons.Close();
        }

        return 0;
    }
}

In the same way, you can use a finally section to free resources used when reading from a stream:

using System;
using System.IO;

public class Program
{
    static int Main(string[] args)
    {
/*      var Filename = "Members.clb";
        var fstPersons = new FileStream(Filename, FileMode.Create);
        var wrtPersons = new BinaryWriter(fstPersons);
        
        try 
        {
            wrtPersons.Write("James Bloch");
            wrtPersons.Write("Catherina Wallace");
            wrtPersons.Write("Bruce Lamont");
            wrtPersons.Write("Douglas Truth");
        }
        finally
        {
            wrtPersons.Close();
            fstPersons.Close();
        }*/

        var strLine = "";
        var Filename = "Members.clb";
        var fstMembers = new FileStream(Filename, FileMode.Open);
        var rdrMembers = new BinaryReader(fstMembers);

        try
        {
            strLine = rdrMembers.ReadString();
            Console.WriteLine(strLine);
            strLine = rdrMembers.ReadString();
            Console.WriteLine(strLine);
            strLine = rdrMembers.ReadString();
            Console.WriteLine(strLine);
            strLine = rdrMembers.ReadString();
            Console.WriteLine(strLine);
        }
        finally
        {
            rdrMembers.Close();
            fstMembers.Close();
        }

        return 0;
    }
}

Of course, since the whole block of code starts with a try section, it is used for exception handling. This means that you can add the necessary and appropriate catch section(s) but you don't have to.

ApplicationApplication: Finally Releasing Resources

  1. Start Microsoft Visual Studio and create a Console Application named IceCream4
  2. To save the project, on the Standard toolbar, click the Save All button
  3. Change the Solution Name to VendingMachine4
  4. Accept the Name of the project as IceCream3 and click Save
  5. To create a new class, on the main menu, click Project -> Add Class...
  6. Set the Name to IceCream and click Add
  7. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace IceCream4
    {
        delegate void Request();
    
        // This class is used to create and manage an Ice Cream
        // and to process an order
        public sealed class IceCream
        {
            // This is the base price of an Ice Cream
            // Optional values may be added to it
            public const decimal BasePrice = 1.55M;
    
            // These arrays are used to build the components 
            // of various ice creams
            private string[] Flavor;
            private string[] Container;
            private string[] Ingredient;
    
            // Additional factor used to process an Ice Cream order
            private int Scoops;
            private decimal TotalPrice;
    
            // Variables that will hold the user's choice
            // These are declared "globally" so they can be 
            // shared among methods
            int ChoiceFlavor;
            int ChoiceContainer;
            int ChoiceIngredient;
    
            // This default constructor is the best place for 
            // us to initialize the array
            public IceCream()
            {
                Flavor = new string[10];
                Flavor[0] = "Vanilla";
                Flavor[1] = "Cream of Cocoa";
                Flavor[2] = "Chocolate Chip";
                Flavor[3] = "Organic Strawberry";
                Flavor[4] = "Butter Pecan";
                Flavor[5] = "Cherry Coke";
                Flavor[6] = "Chocolate Brownies";
                Flavor[7] = "Caramel Au Lait";
                Flavor[8] = "Chunky Butter";
                Flavor[9] = "Chocolate Cookie";
    
                Ingredient = new string[4];
                Ingredient[0] = "No Ingredient";
                Ingredient[1] = "Peanuts";
                Ingredient[2] = "M & M";
                Ingredient[3] = "Cookies";
    
                Container = new string[3];
                Container[0] = "Cone";
                Container[1] = "Cup";
                Container[2] = "Bowl";
            }
    
            // This method requests a flavor from the user and
            // returns the choice
            internal void ChooseFlavor()
            {
                // Make sure the user selects a valid number 
                //that represents a flavor...
                do
                {
                    // In case the user types a symbol that 
                    // is not a number
                    try
                    {
                        Console.WriteLine("What type of flavor do you want?");
                        for (int i = 0; i < Flavor.Length; i++)
                            Console.WriteLine("{0} - {1}", i + 1, Flavor[i]);
                        Console.Write("Your Choice? ");
                        ChoiceFlavor = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)	// display an appropriate message
                    {
                        Console.WriteLine("You must enter a valid number " +
                                          "and no other character!");
                    }
    
                    // If the user typed an invalid number out of the 
                    // allowed range
                    // let him or her know and provide another chance
                    if (ChoiceFlavor < 1 || ChoiceFlavor > Flavor.Length)
                        Console.WriteLine("Invalid Choice - Try Again!\n");
                } while (ChoiceFlavor < 1 || ChoiceFlavor > Flavor.Length);
            }
    
            // This method allows the user to select a container
            internal void ChooseContainer()
            {
                // Make sure the user selects a valid number that 
                // represents a container
                do
                {
                    // If the user types a symbol that is not a number
                    try
                    {
                        Console.WriteLine("What type of container do you want?");
                        for (int i = 0; i < Container.Length; i++)
                            Console.WriteLine("{0} - {1}", i + 1, Container[i]);
                        Console.Write("Your Choice? ");
                        ChoiceContainer = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)	// display an appropriate message
                    {
                        Console.WriteLine("You must enter a valid " +
                                          "number and no other character!");
                    }
    
                    // If the user typed an invalid number out of the 
                    // allowed range
                    // let him or her know and provide another chance
                    if ((ChoiceContainer < 1) ||
                        (ChoiceContainer > Container.Length))
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while ((ChoiceContainer < 1) ||
                         (ChoiceContainer > Container.Length));
            }
    
            internal void ChooseIngredient()
            {
                do
                {
                    try
                    {
                        Console.WriteLine("Do you want an ingredient or not");
                        for (int i = 0; i < Ingredient.Length; i++)
                            Console.WriteLine("{0} - {1}",
                                              i + 1,
                                              Ingredient[i]);
                        Console.Write("Your Choice? ");
                        ChoiceIngredient = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a valid " +
                                          "number and no other character!");
                    }
    
                    if ((ChoiceIngredient < 1) ||
                        (ChoiceIngredient > Ingredient.Length))
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while ((ChoiceIngredient < 1) ||
                         (ChoiceIngredient > Ingredient.Length));
            }
    
            internal void SpecifyNumberOfScoops()
            {
                do
                {
                    try
                    {
                        Console.Write("How many scoops(1, 2, or 3)? ");
                        Scoops = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a valid number " +
                                          "and no other character!");
                    }
    
                    if (Scoops < 1 || Scoops > 3)
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while (Scoops < 1 || Scoops > 3);
            }
    
            // This method is used to process a customer order
            // It uses the values of the above methods
            internal void ProcessAnOrder()
            {
                //			int ChoiceFlavor;
                //			int ChoiceContainer;
                //			int ChoiceIngredient;
                decimal PriceIngredient, PriceScoop;
    
                // Let the user know that this is a vending machine
                Console.WriteLine("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
                Console.WriteLine("Ice Cream Vending Machine");
                Console.WriteLine("-----------------------------------");
    
                // Let the user select the components of the Ice Cream
                Request Get = new Request(ChooseFlavor);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(ChooseContainer);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(ChooseIngredient);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(SpecifyNumberOfScoops);
                Get();
                Console.WriteLine("-----------------------------------");
    
                // If the user selects an ingredient instead of "No Ingredient",
                // add $0.50 to the order
                if ((ChoiceIngredient == 2) ||
                    (ChoiceIngredient == 3) ||
                    (ChoiceIngredient == 4))
                    PriceIngredient = 0.50M;
                else
                    PriceIngredient = 0.00M;
    
                // Instead of multiplying a number scoops to a value,
                // We will use an incremental value depending on 
                // the number of scoops
                if (Scoops == 1)
                    PriceScoop = 0.65M;
                else if (Scoops == 2)
                    PriceScoop = 1.05M;
                else
                    PriceScoop = 1.55M;
    
                // Calculate the total price of the Ice Cream
                TotalPrice = BasePrice + PriceScoop + PriceIngredient;
    
                // Create the Ice Cream...
    
                // And display a receipt to the user
                DisplayReceipt();
            }
    
            // This method is used to display a receipt to the user
            internal void DisplayReceipt()
            {
                Console.WriteLine("\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
                Console.WriteLine("Ice Cream Order");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Flavor:      {0}",
                                  Flavor[ChoiceFlavor - 1]);
                Console.WriteLine("Container:   {0}",
                                  Container[ChoiceContainer - 1]);
                Console.WriteLine("Ingredient:  {0}",
                                  Ingredient[ChoiceIngredient - 1]);
                Console.WriteLine("Scoops:      {0}", Scoops);
                Console.WriteLine("Total Price: {0:C}", TotalPrice);
                Console.WriteLine("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
            }
    
            internal void SaveOrder()
            {
                string strFilename;
    
                Console.Write("Please enter your initials or the name " +
                                  "we will use to remember your order: ");
                strFilename = Console.ReadLine();
                strFilename = strFilename + ".icr";
    
                // Find out if the user entered a name of a file 
                // that is already in the machine
                if (File.Exists(strFilename))
                {
                    char answer;
    
                    FileStream stmIceCream =
                        new FileStream(strFilename, FileMode.Create);
                    BinaryWriter bnwIceCream =
                        new BinaryWriter(stmIceCream);
    
                    try
                    {
    
                        // If so, find out if the user wants to 
                        // replace the old file
                        Console.WriteLine("The file you entered exists already.");
                        Console.Write("Do you want to replace it(y/n)?");
                        answer = char.Parse(Console.ReadLine());
    
                        // If the customer wants to replace it...
                        if (answer == 'y' || answer == 'Y')
                        {
                            // ... do so
                            Console.WriteLine("The former order with the same " +
                                              "name will be replaced");
    
                            Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                            Console.WriteLine(" Saving Order: {0}", strFilename);
                            bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                            bnwIceCream.Write(Container[ChoiceContainer - 1]);
                            bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                            bnwIceCream.Write(Scoops);
                            bnwIceCream.Write(TotalPrice);
                        }
                        // If the customer wants to save the new order with 
                        // a different name
                        else if (answer == 'n' || answer == 'N')
                        {
                            // Ask the user to enter a name to remember the order
                            Console.Write("Please enter a name we will use " +
                                          "to remember this order: ");
                            strFilename = Console.ReadLine();
                            strFilename = strFilename + ".icr";
    
                            stmIceCream = 
    			    new FileStream(strFilename, FileMode.Create);
                            bnwIceCream = new BinaryWriter(stmIceCream);
    
                            Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                            Console.WriteLine(" Saving Order: {0}", strFilename);
                            bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                            bnwIceCream.Write(Container[ChoiceContainer - 1]);
                            bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                            bnwIceCream.Write(Scoops);
                            bnwIceCream.Write(TotalPrice);
                        }
                        else
                            Console.WriteLine("Invalid Answer - We will close");
                    }
                    finally
                    {
                        bnwIceCream.Close();
                        stmIceCream.Close();
                    }
                }
                else
                {
                    FileStream stmIceCream =
                        new FileStream(strFilename, FileMode.Create);
                    BinaryWriter bnwIceCream =
                        new BinaryWriter(stmIceCream);
    
                    try
                    {
                        Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                        Console.WriteLine(" Saving Order: {0}", strFilename);
                        bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                        bnwIceCream.Write(Container[ChoiceContainer - 1]);
                        bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                        bnwIceCream.Write(Scoops);
                        bnwIceCream.Write(TotalPrice);
                    }
                    finally
                    {
                        bnwIceCream.Close();
                        stmIceCream.Close();
                    }
                }
            }
    
            internal void OpenOrder()
            {
                string strFilename;
    
                string SelectedFlavor;
                string SelectedContainer;
                string SelectedIngredient;
    
                // Ask the user to enter a name of a previously saved order
                Console.Write("Please enter the name you previously " +
                              "gave to remember your order: ");
                strFilename = Console.ReadLine();
                strFilename = strFilename + ".icr";
    
                FileStream stmIceCream =
                    new FileStream(strFilename, FileMode.Open);
                BinaryReader bnrIceCream =
                    new BinaryReader(stmIceCream);
    
                try
                {
                    // Find out if this order was previously saved in the machine
                    if (File.Exists(strFilename))
                    {
                        // If so, open it
                        SelectedFlavor = bnrIceCream.ReadString();
                        SelectedContainer = bnrIceCream.ReadString();
                        SelectedIngredient = bnrIceCream.ReadString();
                        Scoops = bnrIceCream.ReadInt32();
                        TotalPrice = bnrIceCream.ReadDecimal();
    
                        // And display it to the user
                        Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                        Console.WriteLine(" Previous Order: {0}", strFilename);
                        Console.WriteLine("Flavor:      {0}", SelectedFlavor);
                        Console.WriteLine("Container:   {0}", SelectedContainer);
                        Console.WriteLine("Ingredient:  {0}", SelectedIngredient);
                        Console.WriteLine("Scoops:      {0}", Scoops);
                        Console.WriteLine("Total Price: {0:C}\n", TotalPrice);
                    }
                    else
                        Console.WriteLine("The name you entered is not " +
                                          "registered in our previous orders");
                }
                finally
                {
                    bnrIceCream.Close();
                    stmIceCream.Close();
                }
            }
        }
    }
    Access the Program.cs file and change it as follows: 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace IceCream4
    {
        public class Program
        {
            static void Main(string[] args)
            {
                var answer = 'n';
                var ic = new IceCream();
                var process = new Request(ic.ProcessAnOrder);
    
                Console.Write("Do you want to re-order a previously " +
                              "saved order(y/n)? ");
                answer = char.Parse(Console.ReadLine());
    
                if (answer == 'y' || answer == 'Y')
                    ic.OpenOrder();
                else
                {
                    process();
                    Console.Write("Do you want us to remember this " +
                                  "order the next time you come to " +
                                  "get your Ice Cream (y/n)? ");
                    answer = char.Parse(Console.ReadLine());
                    if (answer == 'y' || answer == 'Y')
                        ic.SaveOrder();
                    else
                        Console.WriteLine("\nIt was nice serving you." +
                                          "\nCome Again!!!\n");
                }
            }
        }
    }
  8. Execute the application and test it. Here is an example:
    Do you want to re-order a previously saved order(y/n)? n
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Cream Vending Machine
    -----------------------------------
    What type of flavor do you want?
    1 - Vanilla
    2 - Cream of Cocoa
    3 - Chocolate Chip
    4 - Organic Strawberry
    5 - Butter Pecan
    6 - Cherry Coke
    7 - Chocolate Brownies
    8 - Caramel Au Lait
    9 - Chunky Butter
    10 - Chocolate Cookie
    Your Choice? 6
    -----------------------------------
    What type of container do you want?
    1 - Cone
    2 - Cup
    3 - Bowl
    Your Choice? 2
    -----------------------------------
    Do you want an ingredient or not
    1 - No Ingredient
    2 - Peanuts
    3 - M & M
    4 - Cookies
    Your Choice? 1
    -----------------------------------
    How many scoops(1, 2, or 3)? 2
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Cream Order
    -----------------------------------
    Flavor:      Cherry Coke
    Container:   Cup
    Ingredient:  No Ingredient
    Scoops:      2
    Total Price: $2.60
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time you 
    come to get your Ice Cream (y/n)? n
    
    It was nice serving you.
    Come Again!!!
    
    Press any key to continue . . .
  9. Close the DOS window
  10. Execute the application and test it. Indicate that you want to open an existing file
  11. Enter a wrong file name such as Q4 and press Enter
     

     
    Click Don't Send. Here is an example:
    Do you want to re-order a previously saved order(y/n)? y
    Please enter the name you previously gave to remember your order: Q4
    
    Unhandled Exception: System.IO.FileNotFoundException: Could not find file 'C:\Do
    cuments and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Vend
    ingMachine4\IceCream4\bin\Release\Q4.icr'.
    File name: 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2
    008\Projects\VendingMachine4\IceCream4\bin\Release\Q4.icr'
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
    nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
    ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode)
       at IceCream4.IceCream.OpenOrder() in C:\Documents and Settings\Administrator\
    My Documents\Visual Studio 2008\Projects\VendingMachine4\IceCream4\IceCream.cs:l
    ine 362
       at IceCream4.Program.Main(String[] args) in C:\Documents and Settings\Adminis
    trator\My Documents\Visual Studio 2008\Projects\VendingMachine4\IceCream4\Progra
    m.cs:line 21
    Press any key to continue . . .
  12. Close the DOS window

.NET Framework Exception Handling for File Processing

In the previous lesson as our introduction to file processing, we behaved as if everything was alright. Unfortunately, file processing can be very strict in its assignments. Based on this, the .NET Framework provides various Exception-oriented classes to deal with almost any type of exception you can think of.

One of the most important aspects of file processing is the name of the file that will be dealt with. In some cases you can provide this name to the application or document. In some other cases, you would let the user specify the name of the path. Regardless of how the name of the file would be provided to the operating system, when this name is acted upon, the compiler is asked to work on the file. If the file doesn't exist, the operation cannot be carried. Furthermore, the compiler would throw an error. There are many other exceptions that can be thrown as a result of something going bad during file processing:

FileNotFoundException: The exception thrown when a file has not been found is of type FileNotFoundException. Here is an example of handling it:

using System;
using System.IO;

public class Program
{
    static int Main(string[] args)
    {
/*      var Filename = "Members.clb";
        var fstPersons = 
            new FileStream(Filename, FileMode.Create);
        var wrtPersons = new BinaryWriter(fstPersons);
        
        try 
        {
            wrtPersons.Write("James Bloch");
            wrtPersons.Write("Catherina Wallace");
            wrtPersons.Write("Bruce Lamont");
            wrtPersons.Write("Douglas Truth");
        }
        finally
        {
            wrtPersons.Close();
            fstPersons.Close();
        }*/


        var strLine = "";
        var Filename = "Members.clc";

        try
        {
            var fstMembers =
                new FileStream(Filename, FileMode.Open);
            var rdrMembers = new BinaryReader(fstMembers);

            try
            {
                strLine = rdrMembers.ReadString();
                Console.WriteLine(strLine);
                strLine = rdrMembers.ReadString();
                Console.WriteLine(strLine);
                strLine = rdrMembers.ReadString();
                Console.WriteLine(strLine);
                strLine = rdrMembers.ReadString();
                Console.WriteLine(strLine);
            }
            finally
            {
                rdrMembers.Close();
                fstMembers.Close();
            }
        }
        catch (FileNotFoundException ex)
        {
            Console.Write("Error: " + ex.Message);
            Console.WriteLine(" May be the file doesn't exist " +
                              "or you typed it wrong!");
        }

        return 0;
    }
}

Here is an example of what this would produce:

Error: Could not find file 'C:\Documents and Settings\Administrator\Local 
Settings\Application Data\Temporary 
Projects\ConsoleApplication1\bin\Release\Members.clc'. 
May be the file doesn't exist or you typed it wrong!
Press any key to continue . . .

IOException: As mentioned already, during file processing, anything could go wrong. If you don't know what caused an error, you can throw the IOException exception.

ApplicationApplication: Handling File Processing Exceptions

  1. To throw exceptions, change the file processing methods from the IceCream.cs file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace IceCream4
    {
        delegate void Request();
    
        // This class is used to create and manage an Ice Cream
        // and to process an order
        public sealed class IceCream
        {
            // This is the base price of an Ice Cream
            // Optional values may be added to it
            public const decimal BasePrice = 1.55M;
    
            // These arrays are used to build the components 
            // of various Ice Creams
            // In C#, we can allocate an array's memory in 
            // the body of the class
            private string[] Flavor;
            private string[] Container;
            private string[] Ingredient;
    
            // Additional factor used to process an Ice Cream order
            private int Scoops;
            private decimal TotalPrice;
    
            // Variables that will hold the user's choice
            // These are declared "globally" so they can be 
            // shared among methods
            int ChoiceFlavor;
            int ChoiceContainer;
            int ChoiceIngredient;
    
            // This default constructor is the best place for 
            // us to initialize the array
            public IceCream()
            {
                Flavor = new string[10];
                Flavor[0] = "Vanilla";
                Flavor[1] = "Cream of Cocoa";
                Flavor[2] = "Chocolate Chip";
                Flavor[3] = "Organic Strawberry";
                Flavor[4] = "Butter Pecan";
                Flavor[5] = "Cherry Coke";
                Flavor[6] = "Chocolate Brownies";
                Flavor[7] = "Caramel Au Lait";
                Flavor[8] = "Chunky Butter";
                Flavor[9] = "Chocolate Cookie";
    
                Ingredient = new string[4];
                Ingredient[0] = "No Ingredient";
                Ingredient[1] = "Peanuts";
                Ingredient[2] = "M & M";
                Ingredient[3] = "Cookies";
    
                Container = new string[3];
                Container[0] = "Cone";
                Container[1] = "Cup";
                Container[2] = "Bowl";
            }
    
            // This method requests a flavor from the user and
            // returns the choice
            internal void ChooseFlavor()
            {
                // Make sure the user selects a valid number 
                //that represents a flavor...
                do
                {
                    // In case the user types a symbol that 
                    // is not a number
                    try
                    {
                        Console.WriteLine("What type of flavor do you want?");
                        for (int i = 0; i < Flavor.Length; i++)
                            Console.WriteLine("{0} - {1}", i + 1, Flavor[i]);
                        Console.Write("Your Choice? ");
                        ChoiceFlavor = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)	// display an appropriate message
                    {
                        Console.WriteLine("You must enter a valid number " +
                                          "and no other character!");
                    }
    
                    // If the user typed an invalid number out of the 
                    // allowed range
                    // let him or her know and provide another chance
                    if (ChoiceFlavor < 1 || ChoiceFlavor > Flavor.Length)
                        Console.WriteLine("Invalid Choice - Try Again!\n");
                } while (ChoiceFlavor < 1 || ChoiceFlavor > Flavor.Length);
            }
    
            // This method allows the user to select a container
            internal void ChooseContainer()
            {
                // Make sure the user selects a valid number that 
                // represents a container
                do
                {
                    // If the user types a symbol that is not a number
                    try
                    {
                        Console.WriteLine("What type of container do you want?");
                        for (int i = 0; i < Container.Length; i++)
                            Console.WriteLine("{0} - {1}", i + 1, Container[i]);
                        Console.Write("Your Choice? ");
                        ChoiceContainer = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)	// display an appropriate message
                    {
                        Console.WriteLine("You must enter a valid " +
                                          "number and no other character!");
                    }
    
                    // If the user typed an invalid number out of the 
                    // allowed range
                    // let him or her know and provide another chance
                    if ((ChoiceContainer < 1) ||
                        (ChoiceContainer > Container.Length))
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while ((ChoiceContainer < 1) ||
                         (ChoiceContainer > Container.Length));
            }
    
            internal void ChooseIngredient()
            {
                do
                {
                    try
                    {
                        Console.WriteLine("Do you want an ingredient or not");
                        for (int i = 0; i < Ingredient.Length; i++)
                            Console.WriteLine("{0} - {1}",
                                              i + 1,
                                              Ingredient[i]);
                        Console.Write("Your Choice? ");
                        ChoiceIngredient = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a valid " +
                                          "number and no other character!");
                    }
    
                    if ((ChoiceIngredient < 1) ||
                        (ChoiceIngredient > Ingredient.Length))
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while ((ChoiceIngredient < 1) ||
                         (ChoiceIngredient > Ingredient.Length));
            }
    
            internal void SpecifyNumberOfScoops()
            {
                do
                {
                    try
                    {
                        Console.Write("How many scoops(1, 2, or 3)? ");
                        Scoops = int.Parse(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a valid number " +
                                          "and no other character!");
                    }
    
                    if (Scoops < 1 || Scoops > 3)
                        Console.WriteLine("Invalid Choice - Try Again!");
                } while (Scoops < 1 || Scoops > 3);
            }
    
            // This method is used to process a customer order
            // It uses the values of the above methods
            public void ProcessAnOrder()
            {
                //			int ChoiceFlavor;
                //			int ChoiceContainer;
                //			int ChoiceIngredient;
                decimal PriceIngredient, PriceScoop;
    
                // Let the user know that this is a vending machine
                Console.WriteLine("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
                Console.WriteLine("Ice Cream Vending Machine");
                Console.WriteLine("-----------------------------------");
    
                // Let the user select the components of the Ice Cream
                Request Get = new Request(ChooseFlavor);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(ChooseContainer);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(ChooseIngredient);
                Get();
                Console.WriteLine("-----------------------------------");
                Get = new Request(SpecifyNumberOfScoops);
                Get();
                Console.WriteLine("-----------------------------------");
    
                // If the user selects an ingredient instead of "No Ingredient",
                // add $0.50 to the order
                if ((ChoiceIngredient == 2) ||
                    (ChoiceIngredient == 3) ||
                    (ChoiceIngredient == 4))
                    PriceIngredient = 0.50M;
                else
                    PriceIngredient = 0.00M;
    
                // Instead of multiplying a number scoops to a value,
                // We will use an incremental value depending on 
                // the number of scoops
                if (Scoops == 1)
                    PriceScoop = 0.65M;
                else if (Scoops == 2)
                    PriceScoop = 1.05M;
                else
                    PriceScoop = 1.55M;
    
                // Calculate the total price of the Ice Cream
                TotalPrice = BasePrice + PriceScoop + PriceIngredient;
    
                // Create the Ice Cream...
    
                // And display a receipt to the user
                DisplayReceipt();
            }
    
            // This method is used to display a receipt to the user
            internal void DisplayReceipt()
            {
                Console.WriteLine("\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
                Console.WriteLine("Ice Cream Order");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Flavor:      {0}",
                                  Flavor[ChoiceFlavor - 1]);
                Console.WriteLine("Container:   {0}",
                                  Container[ChoiceContainer - 1]);
                Console.WriteLine("Ingredient:  {0}",
                                  Ingredient[ChoiceIngredient - 1]);
                Console.WriteLine("Scoops:      {0}", Scoops);
                Console.WriteLine("Total Price: {0:C}", TotalPrice);
                Console.WriteLine("=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
            }
    
            internal void SaveOrder()
            {
                string strFilename;
    
                Console.Write("Please enter your initials or the name " +
                                  "we will use to remember your order: ");
                strFilename = Console.ReadLine();
                strFilename = strFilename + ".icr";
    
                try
                {
                    // Find out if the user entered a name of a file 
                    // that is already in the machine
                    if (File.Exists(strFilename))
                    {
                        char answer;
    
                        FileStream stmIceCream =
                            new FileStream(strFilename, FileMode.Create);
                        BinaryWriter bnwIceCream =
                            new BinaryWriter(stmIceCream);
    
                        try
                        {
    
                            // If so, find out if the user wants to 
                            // replace the old file
                            Console.WriteLine("The file you entered exists already.");
                            Console.Write("Do you want to replace it(y/n)?");
                            answer = char.Parse(Console.ReadLine());
    
                            // If the customer wants to replace it...
                            if (answer == 'y' || answer == 'Y')
                            {
                                // ... do so
                                Console.WriteLine("The former order with the same " +
                                                  "name will be replaced");
    
                                Console.WriteLine("\n=-= Ice Cream " +
    						"Vending Machine =-=");
                                Console.WriteLine(" Saving Order: {0}", strFilename);
                                bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                                bnwIceCream.Write(Container[ChoiceContainer - 1]);
                                bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                                bnwIceCream.Write(Scoops);
                                bnwIceCream.Write(TotalPrice);
                            }
                            // If the customer wants to save the new order with 
                            // a different name
                            else if (answer == 'n' || answer == 'N')
                            {
                                // Ask the user to enter a name to remember the order
                                Console.Write("Please enter a name we will use " +
                                              "to remember this order: ");
                                strFilename = Console.ReadLine();
                                strFilename = strFilename + ".icr";
    
                                try
                                {
                                    stmIceCream = new FileStream(strFilename,
    							 FileMode.Create);
                                    bnwIceCream = new BinaryWriter(stmIceCream);
    
                                    Console.WriteLine("\n=-= Ice Cream " +
    						  "Vending Machine =-=");
                                    Console.WriteLine(" Saving Order: {0}",
    						 strFilename);
                                    bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                                bnwIceCream.Write(Container[ChoiceContainer - 1]);
                                bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                                    bnwIceCream.Write(Scoops);
                                    bnwIceCream.Write(TotalPrice);
                                }
                                catch (IOException)
                                {
                                    Console.Write("\nThe file you wanted us to " +
    						"create exists already. ");
                                    Console.WriteLine("In case it was registered " +
    						  "by a different customer, " +
    						  "we will not delete it.");
                                }
                            }
                            else
                                Console.WriteLine("Invalid Answer - We will close");
                        }
                        catch (IOException)
                        {
                            Console.Write("\nThat file exists already. ");
                            Console.WriteLine("We need to preserve it just in " +
    					"case another customer will require it.");
                        }
                        finally
                        {
                            bnwIceCream.Close();
                            stmIceCream.Close();
                        }
                    }
                    else
                    {
                        FileStream stmIceCream =
                            new FileStream(strFilename, FileMode.Create);
                        BinaryWriter bnwIceCream =
                            new BinaryWriter(stmIceCream);
    
                        try
                        {
                            Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                            Console.WriteLine(" Saving Order: {0}", strFilename);
                            bnwIceCream.Write(Flavor[ChoiceFlavor - 1]);
                            bnwIceCream.Write(Container[ChoiceContainer - 1]);
                            bnwIceCream.Write(Ingredient[ChoiceIngredient - 1]);
                            bnwIceCream.Write(Scoops);
                            bnwIceCream.Write(TotalPrice);
                        }
                        finally
                        {
                            bnwIceCream.Close();
                            stmIceCream.Close();
                        }
                    }
                }
                catch (IOException ex)
                {
                    Console.WriteLine("\nError: " + ex.Message);
                    Console.WriteLine("Operation Canceled: The file you want " +
    					"to create exists already.");
                }
            }
    
            internal void OpenOrder()
            {
                string strFilename;
    
                string SelectedFlavor;
                string SelectedContainer;
                string SelectedIngredient;
    
                // Ask the user to enter a name of a previously saved order
                Console.Write("Please enter the name you previously " +
                              "gave to remember your order: ");
                strFilename = Console.ReadLine();
                strFilename = strFilename + ".icr";
    
                try
                {
                    FileStream stmIceCream =
                        new FileStream(strFilename, FileMode.Open);
                    BinaryReader bnrIceCream =
                        new BinaryReader(stmIceCream);
    
                    try
                    {
                        // Find out if this order was previously saved in the machine
                        if (File.Exists(strFilename))
                        {
                            // If so, open it
                            SelectedFlavor = bnrIceCream.ReadString();
                            SelectedContainer = bnrIceCream.ReadString();
                            SelectedIngredient = bnrIceCream.ReadString();
                            Scoops = bnrIceCream.ReadInt32();
                            TotalPrice = bnrIceCream.ReadDecimal();
    
                            // And display it to the user
                            Console.WriteLine("\n=-= Ice Cream Vending Machine =-=");
                            Console.WriteLine(" Previous Order: {0}", strFilename);
                            Console.WriteLine("Flavor:      {0}", SelectedFlavor);
                            Console.WriteLine("Container:   {0}", SelectedContainer);
                            Console.WriteLine("Ingredient:  {0}", SelectedIngredient);
                            Console.WriteLine("Scoops:      {0}", Scoops);
                            Console.WriteLine("Total Price: {0:C}\n", TotalPrice);
                        }
                        else
                            Console.WriteLine("The name you entered is not " +
                                              "registered in our previous orders");
                    }
                    finally
                    {
                        bnrIceCream.Close();
                        stmIceCream.Close();
                    }
                }
                catch (FileNotFoundException ex)
                {
                    Console.Write("\nError: " + ex.Message);
                    Console.WriteLine("It is unlikely that the file exists in " +
    				  "our machine's register.");
                }
            }
        }
    }
  2. Exercise the application and test it
  3. Close the DOS window

File Information

 

Introduction

In its high level of support for file processing, the .NET Framework provides the FileInfo class. This class is equipped to handle all types of file-related operations including creating, copying, moving, renaming, or deleting a file. FileInfo is based on the FileSystemInfo class that provides information on characteristics of a file.

ApplicationApplication: Introducing File Information

  1. Start a new Console Application named WattsALoan2
  2. To take advantage of the Visual Basic rich library, in the Solution Explorer, under WattsALoan2, right-click References and click Add Reference
  3. In the Add Reference dialog box, click the .NET tab and click Microsoft.VisualBasic
     
    Add Reference
  4. Click OK
  5. Change the Program.cs file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace WattsALoan1
    {
        public class Program
        {
            static int Main(string[] args)
            {
                string EmployerName, ApplicantName;
                string HomePhone, WorkPhone;
                double LoanAmount = 0.00D, InterestRate = 0.0D;
                double MonthlyPayment = 0.00D, Periods = 0D;
    
                Console.WriteLine(" -=- Car Loan Application -=-");
                Console.WriteLine("Enter the following pieces " +
                                  "of information\n");
                Console.WriteLine("Applicant Information");
                Console.Write("Full Name:     ");
                ApplicantName = Console.ReadLine();
                Console.Write("Employer Name: ");
                EmployerName = Console.ReadLine();
                Console.Write("Home Phone:    ");
                HomePhone = Console.ReadLine();
                Console.Write("Work Phone:    ");
                WorkPhone = Console.ReadLine();
                Console.WriteLine("Loan Estimation");
    
                try
                {
                    Console.Write("Amount of Loan: ");
                    LoanAmount = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Loan Amount");
                }
                try
                {
                    Console.Write("Interest Rate(0 to 100): ");
                    InterestRate = double.Parse(Console.ReadLine()) / 100;
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Interest Rate");
                }
                try
                {
                    Console.Write("Number of Months: ");
                    Periods = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Number of Months");
                }
    
                try
                {
                    MonthlyPayment =
                    Microsoft.VisualBasic.Financial.Pmt(InterestRate / 12,
                        Periods, -LoanAmount,
                        0, Microsoft.VisualBasic.DueDate.BegOfPeriod);
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("Some invalid values were provided\n");
                }
    
                Console.WriteLine();
                Console.WriteLine(" -=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                Console.WriteLine(" -=- Car Loan Application -=-");
                Console.WriteLine(" -=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                Console.WriteLine("Applicant Information");
                Console.WriteLine("Full Name:     {0}", ApplicantName);
                Console.WriteLine("Employer Name: {0}", EmployerName);
                Console.WriteLine("Home Phone:    {0}", HomePhone);
                Console.WriteLine("Work Phone:    {0}", WorkPhone);
                Console.WriteLine("Loan Estimation");
                Console.WriteLine("Loan Amount:      {0:C}", LoanAmount);
                Console.WriteLine("Interest Rate:    {0:P}", InterestRate);
                Console.WriteLine("Number of Months: {0:F}", Periods);
                Console.WriteLine("Monthly Payment:  {0:C}\n", MonthlyPayment);
            }
        }
    }
  6. Execute the application and test it. Here is an example:
     -=- Car Loan Application -=-
    Enter the following pieces of information
    
    Applicant Information
    Full Name:     James Watts
    Employer Name: Wattson Enterprises
    Home Phone:    (202) 374-4738
    Work Phone:    (301) 894-4789
    Loan Estimation
    Amount of Loan: 12500
    Interest Rate(0 to 100): 10.25
    Number of Months: 48
    
     -=-=-=-=-=-=-=-=-=-=-=-=-=-=
     -=- Car Loan Application -=-
     -=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Applicant Information
    Full Name:     James Watts
    Employer Name: Wattson Enterprises
    Home Phone:    (202) 374-4738
    Work Phone:    (301) 894-4789
    Loan Estimation
    Loan Amount:      $12,500.00
    Interest Rate:    10.25 %
    Number of Months: 48.00
    Monthly Payment:  $315.84
    Press any key to continue...
  7. Close the DOS window

File Initialization

The FileInfo class is equipped with one constructor whose syntax is:

public FileInfo(string fileName);

This constructor takes as argument the name of a file or its complete path. If you provide only the name of the file, the compiler would consider the same directory of its project. Here is an example:

FileInfo fleMembers = new FileInfo("First.txt");

Alternatively, if you want, you can provide any valid directory you have access to. In this case, you should provide the complete path.

File Creation

The FileInfo constructor is mostly meant only to indicate that you want to use a file, whether it exists already or it would be created. Based on this, if you execute an application that has only a FileInfo object created using the constructor as done above, nothing would happen.

To create a file, you have various alternatives. If you want to create one without writing anything in it, which implies creating an empty file, you can call the FileInfo.Create() method. Its syntax is:

public FileStream Create();

This method simply creates an empty file. Here is an example of calling it:

using System;
using System.IO;

public class Program
{
    static int Main(string[] args)
    {
        var fleMembers = new FileInfo("First.txt");
        fleMembers.Create();

        return 0;
    }
}

The FileInfo.Create() method returns a FileStream object. You can use this returned value to write any type of value into the file, including text. If you want to create a file that contains text, an alternative is to call the FileInfo.CreateText() method. Its syntax is:

public StreamWriter CreateText();

This method directly returns a StreamWriter object. You can use this returned object to write text to the file.

File Existence

When you call the FileInfo.Create() or the FileInfo.CreateText() method, if the file passed as argument, or as the file in the path of the argument, exists already, it would be deleted and a new one would be created with the same name. This can cause the right file to be deleted. Therefore, before creating a file, you may need to check whether it exists already. To do this, you can check the value of the Boolean FileInfo.Exists property. This property holds a true value if the file exists already and it holds a false value if the file doesn't exist or it doesn't exist in the path.

Here is an example of checking the existence of a file:

using System;
using System.IO;

public class Program
{
    static int Main(string[] args)
    {
        var fleMembers = new FileInfo("First.txt");

        if (fleMembers.Exists == true)
            return 0;
        else
            fleMembers.Create();

        return 0;
    }
}
 

Writing to a File

As mentioned earlier, the FileInfo.Create() method returns a FileStream object. You can use this to specify the type of operation that would be allowed on the file.

To write normal text to a file, you can first call the FileInfo.CreateText() method. This method returns a StreamWriter object. The StreamWriter class is based on the TextWriter class that is equipped with the Write() and the WriteLine() methods used to write values to a file. The Write() method writes text on a line and keeps the caret on the same line. The WriteLine() method writes a line of text and moves the caret to the next line.

After writing to a file, you should close the StreamWriter object to free the resources it was using during its operation(s).

Application Application: Writing to a File

  1. To allow the user to create a new employee, change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace WattsALoan2
    {
        class Program
        {
            private static void CreateNewEmployee()
            {
                string employeeNumber, employeeName;
                FileInfo fleEmployees = new FileInfo("Employees.txt");
                StreamWriter swrEmployees = fleEmployees.CreateText();
    
                try
                {
                    Console.WriteLine("Hiring New Employee");
                    Console.Write("Enter Employee Number as 00-000: ");
                    employeeNumber = Console.ReadLine();
                    Console.Write("Enter Employee Name: ");
                    employeeName = Console.ReadLine();
    
                    swrEmployees.WriteLine(employeeNumber);
                    swrEmployees.WriteLine(employeeName);
    
                }
                finally
                {
                    swrEmployees.Flush();
                    swrEmployees.Close();
                }
            }
    
            static int Main(string[] args)
            {
                try
                {
                    Console.Write("Do you want to hire a new " +
                                  "employee(0=No/1=Yes)? ");
                    int answer = int.Parse(Console.ReadLine());
    
                    if (answer == 1)
                        CreateNewEmployee();
                }
                catch (FormatException)
                {
                    Console.WriteLine("That was an invalid answer!\n");
                }
    
                string EmployerName, ApplicantName;
                string HomePhone, WorkPhone;
                double LoanAmount = 0.00D, InterestRate = 0.0D;
                double MonthlyPayment = 0.00D, Periods = 0D;
    
                . . . No Change
    
                return 0;
            }
        }
    }
  2. Execute the application. Here is an example:
    Do you want to hire a new employee(0=No/1=Yes)? 1
    Hiring New Employee
    Enter Employee Number as 00-000: 44-228
    Enter Employee Name: Johnny Olney
  3. Close the DOS window

Appending to a File

You may have created a text-based file and written to it. If you open such a file and find out that a piece of information is missing, you can add that information to the end of the file. To do this, you can call the FileInfo.AppenText() method. Its syntax is:

public StreamWriter AppendText();

When calling this method, you can retrieve the StreamWriter object that it returns, then use that object to add new information to the file.

Application Application: Appending to a File

  1. To allow the user to add more employees to the file that holds their names, change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace WattsALoan2
    {
        public class Program
        {
            private static void CreateNewEmployee()
            {
                string employeeNumber, employeeName;
                FileInfo fleEmployees = new FileInfo("Employees.txt");
                StreamWriter swrEmployees = null;
    
                // If the file exists already, then add the new employee to it
                if (fleEmployees.Exists == true)
                    swrEmployees = fleEmployees.AppendText();
                else // Otherwise, create a new file
                    swrEmployees = fleEmployees.CreateText();
    
                try
                {
                    Console.WriteLine("Hiring New Employee");
                    Console.Write("Enter Employee Number as 00-000: ");
                    employeeNumber = Console.ReadLine();
                    Console.Write("Enter Employee Name: ");
                    employeeName = Console.ReadLine();
    
                    swrEmployees.WriteLine(employeeNumber);
                    swrEmployees.WriteLine(employeeName);
    
                }
                finally
                {
                    swrEmployees.Flush();
                    swrEmployees.Close();
                }
            }
    
            static void Main(string[] args)
            {
                . . . No Change
            }
        }
    }
  2. Execute the application
  3. Type y to create an employee. Here is an example:
    Do you want to hire a new employee(0=No/1=Yes)? 1
    Hiring New Employee
    Enter Employee Number as 00-000: 72-604
    Enter Employee Name: Ernest Barzan
  4. Close the DOS window
 
 

Previous Copyright © 2010-2016, FunctionX Next