Radio Buttons

 

This chapter deals with a small number of powerful Visual Studio C# statements: FileStream, StreamReader, and StreamWriter. These statements are defined in using System.IO. We show what you can do with these statements and what you cannot do.

FileStream (Creating a File)

During file creation, most programmers want to ensure that the file open /creation process is successful, so they use the modern try catch sequence:

 FileStream fs; try {   fs = new FileStream ("KTWeeklyNetWorth.dta", FileMode.OpenOrCreate,                        FileAccess.ReadWrite); } catch {   MessageBox.Show("Cannot open file "KTWeeklyNetWorth.dta'.");   return; } fs.Close(); 

At this point in the code, the programmer has a handle to a file stream (fs), but there isn t much that can be accomplished with it. The length of the file can be determined at this time, which is always helpful:

 Int FileLength = fs.Length; 

And, as we show in the project CRAndWFiles, there is a read mechanism (see lines CR026-CR039 at the end of this chapter). But all that this read mechanism can read is bytes, not chars nor strings. However, the bytes can be regrouped into chars or strings later (see CR029-CR030). As far as we know, there is no write mechanism supported in this FileStream construct, but this is what FileStream can do for you:

  • Verify that a file is present and openable.

  • Determine the file s length once it is open.

The file opening sequence is always closed with fs.Close().

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net