
C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#
.pdf
Module 10: Data Streams and Files |
21 |
|
|
|
|
Lab 10: Files
Topic Objective
To introduce the lab.
Lead-in
In this lab, you will create an application that reads and writes characters to and from files, and create an application that can use
StringReader and StreamReader objects to read character data from either files or strings.
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Objectives
After completing this lab, you will be able to:
!Create an application that reads and writes characters to and from files.
!Create an application that can use StringReader and StreamReader objects to read character data from files or strings.
Lab Setup
Starter and solution files are associated with this lab. The starter files are in the folder <install folder>\Labs\Lab10\Starter, and the solution files are in the folder <install folder>\Labs\Lab10\Solution.
Scenario
In this lab, you are provided with a Microsoft Visual Studio® .NET console application as a starting point. The application, named Files, opens one or more files, which are specified on the command line, and counts each file’s bytes, characters, words, and lines. The results from each file and the total of all files are displayed on the console.
The application supports an –f switch to allow the output display to be redirected to a file and a –t switch to run a special test mode of operation where input is obtained from a coded-in test string, instead of from user-specified files. The application is based on a slightly modified version of the .NET Framework SDK sample, Word Count.
Estimated time to complete this lab: 45 minutes
Module 10: Data Streams and Files |
23 |
|
|
|
|
8.Replace the following line’s Console.WriteLine call with code that:
a.Assigns fsOut to a FileStream object that creates a file with the specified name with Write access and no file sharing.
b.Assigns sw to a StreamWriter object that is bound to the FileStream object created in step a.
c.Redirects the console’s output to the file by associating the StreamWriter object with the console by using the following command:
Console.SetOut(sw);
9.Rebuild and run the application, and examine the output file that is specified in the command line switch options.
The file should be located in the bin\Debug subdirectory. It should contain the following text:
Lines Words Chars Bytes Pathname
Replace this Console.WriteLine in WordCounter's!
CountStats method |
|
|
||
0 |
0 |
0 |
0 |
Test String |
----- |
----- |
----- |
----- |
--------------------- |
0 |
0 |
0 |
0 |
Total in all files |
Word usage sorted alphabetically (0 unique words)
Word usage sorted by occurrence (0 unique words)
! Add the test mode and normal mode CountStats processing
1.Locate the following lines in the CountStats method code of the
WordCounter class:
Boolean Ok = true;
numLines = numWords = numChars = numBytes = 0; try {
If the –t option has been set, the CountStats caller will have set the pathname parameter to the empty string, which is called String.Empty.
2.Replace the following line’s Console.WriteLine call with code that:
a.Declares a variable of type TextReader and names it tr.
You use the type TextReader because it is the common base type for both StringReader and StreamReader. Polymorphism will allow code that uses a TextReader object to be provided with either a
StringReader or StreamReader object.
b.If the pathname is empty:
i.Create a StringReader that is named sr, which is bound to a member of WordCounter that is named testString.
ii.Assign the number of bytes in this string to numBytes.
iii.Assign sr to tr.
This assignment does an implicit cast of a StringReader to a
TextReader.

24Module 10: Data Streams and Files
c.If the pathname is not empty:
i.Create a FileStream that is named fsIn by opening the file that is specified in the parameter pathname with read access and shared read access.
ii.Assign the number of bytes in fsIn to numBytes.
iii.Create a StreamReader that is named sr that is bound to this file.
iv.Assign sr to tr.
This assignment does an implicit cast of a StreamReader to a
TextReader
d.In this module, uncomment out the for loop that follows the comment:
// Process every line in the file
e. Following the for loop, add code to close the TextReader object.
3.Rebuild and run the application, and examine the output file in the bin\Debug subdirectory. It should contain the following text:
Lines |
Words |
Chars |
Bytes |
Pathname |
2 |
3 |
16 |
17 |
Test String |
----- |
----- |
----- |
----- |
--------------------- |
2 |
3 |
16 |
17 |
Total in all files |
Word usage sorted alphabetically (2 unique words) 2: "hello"
1: "world"
Word usage sorted by occurrence (2 unique words)
1:world
2:hello
4.Change the command line options that Visual Studio .NET will use to run the application to remove the test switch. It should be set to:
-a -o -foutput.txt test.txt
Module 10: Data Streams and Files |
25 |
|
|
|
|
5.Run the application, and examine the output file in the bin\Debug subdirectory. It should contain the following text:
Lines |
Words |
Chars |
Bytes |
Pathname |
5 |
16 |
65 |
73 |
...\test.txt |
----- |
----- |
----- |
----- |
--------------------- |
5 |
16 |
65 |
73 |
Total in all files |
Word usage sorted alphabetically (14 unique words)
1:"aid"
1:"all"
1:"come"
1:"country"
1:"for"
1:"good"
1:"is"
1:"men"
1:"now"
1:"of"
2:"the"
1:"their"
1:"time"
2:"to"
Word usage sorted by occurrence (14 unique words)
1:aid
1:all
1:come
1:country
1:for
1:good
1:is
1:men
1:now
1:of
1:their
1:time
2:the
2:to
6.Examine the file Test.txt in the bin\Debug subdirectory, and verify that the output is what you expected.

Module 10: Data Streams and Files |
27 |
|
|
|
|
5.Name the class that you would use to monitor changes to a file system.
FileSystemWatcher.
6.Name the two important features that the .NET Framework’s isolated storage provides for an application.
Isolation and Safety.

Module 11:
Internet Access
Contents |
|
Overview |
1 |
Internet Application Scenarios |
2 |
The WebRequest and WebResponse Model |
3 |
Application Protocols |
16 |
Handling Errors |
25 |
Security |
28 |
Best Practices |
35 |
Lab 11: Creating a DateTime Client/Server |
|
Application |
36 |
Review |
41 |
Course Evaluation |
43 |
Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
2001-2002 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and
Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective owners.