Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2004, 02:19 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 20 Answers

1. The System.IO namespace contains all of the classes for reading and writing data to files.

2. The FileStream class maintains an internal file pointer allowing random file access via the
        Seek() method. Also, the FileStream class exclusively handles raw byte data making it the
        only handler for non-text files, such as image or sound.

3. The StreamReader class has four methods to pull character data from the input stream:
            Peek() Returns the next available character but does not consume it
            Read() Returns the next available character and advances the character position by one
            ReadLine() Returns the current line and advances the character position to start of next line
            ReadToEnd() Returns characters from the current position to the end of the stream as
                a string

4. The FileSystemWatcher class exposes five events:
            Changed raised when changes are made to the size, system attributes, last write time,
                last access time, or security permissions
            Created raised when a file or directory in the specified path is created
            Deleted raised when a file or directory in the specified path is deleted
            Renamed raised when a file or directory in the specified path is renamed
            Error raised when the internal buffer overflows as a result of too many changes in a short time

5. Rewrite the "cmdWatch_Click()" event handler...
        private void cmdWatch_Click(object sender, EventArgs e)
        {
            if (Watcher.EnableRaisingEvents == true)
            {
                //Stop watching
                Watcher.EnableRaisingEvents = false;
                lblWatch.Text = "";
                txtLocation.Text = "";
                cmdWatch.Enabled = false;
                cmdWatch.Text = "Watch!";
            }
            else
            {
                Watcher.Path = Path.GetDirectoryName(txtLocation.Text);
                Watcher.Filter = Path.GetFileName(txtLocation.Text);
                Watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
                lblWatch.Text = "Watching " + txtLocation.Text;
                // Begin watching
                Watcher.EnableRaisingEvents = true;
                cmdWatch.Text = "Stop";
            }
        }

6. Declare the namespaces...
        using System;
        using System.IO;
        using System.Text;
        using System.Data;
        using System.Data.SqlClient;

    In the Main() function...
        // Specify SQL Server specific connection string
        SqlConnection thisConnection = new
            SqlConnection(@"Data Source=(local);Integrated Security=SSPI;Initial Catalog=northwind");
        SqlDataAdapter thisAdapter = new
            SqlDataAdapter("SELECT CustomerID, ContactName From Customers", thisConnection);
        DataSet thisDataSet = new DataSet();

        // Fill DataSet using query defined previously for DataAdapter
        thisAdapter.Fill(thisDataSet, "Customers");

        try
        {
            // Create a StreamWriter object
            StreamWriter sw = new StreamWriter("customers.txt", false);

            foreach (DataRow theRow in thisDataSet.Tables["Customers"].Rows)
            {
                // comma delimited fields
                sw.WriteLine("{0},{1}", theRow["CustomerID"], theRow["ContactName"]);
            }

            // Write the file
            sw.Close();
        }
        catch (IOException ex)
        {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
            return;
        }
        return;









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.