Group Boxes

 

Project CRAndWFiles

The project shown below, CRAndWFiles, demonstrates all the file create, read, and write principles mentioned above. The project is located at Visual Studio 2005\Projects\DemosSourceCode\CRAndWFiles. Bring the project into the IDE, then compile and run it. The main window looks like the following:

image from book
Figure 17-1: Create, Read, and Write Files main window

When the Begin File Opening With 'FileStream button is clicked, the button1_Click event handler is actuated. KTWeeklyNetWorth.dta is opened and 47 bytes are read from the file (line CR028). Lines CR029-CR030 convert the bytes to individual chars, which are a part of the charBlockData char array. Line CR031 converts the char array to a string, and CR032 prints that array to show that the read was successful. This method for reading a file is not recommended, but this project shows that it will work!

CRAndWFiles (Creating, Reading, and Writing Files) (CR) Listing

 Form1.cs: CR002:        using System; CR003:        using System.Collections.Generic; CR004:        using System.ComponentModel; CR005:        using System.Data; CR006:        using System.Drawing; CR007:        using System.Windows.Forms; CR008:        using System.IO;     // 'FileStream', 'StreamReader', 'StreamWriter'. CR009:        using System.Text;   // 'Encoding'. CR011:        namespace CRAndWFiles CR012:        { CR013:          partial class Form1 : Form CR014:          { CR015:            public Form1() CR016:            {InitializeComponent(); } //-----------------------------------------------------------------------------------------// CR020:            private void button1_Click(object sender, EventArgs e) CR021:            { // Begin File Opening With 'FileStream'. CR022:              FileStream fs; CR023:              try CR024:              { CR025:                fs = new FileStream("WeeklyNetWorth.dta", FileMode.OpenOrCreate,                                FileAccess.ReadWrite);                       // Read from the file. CR026:                byte[] byteBlockData = new byte[47]; CR027:                char[] charBlockData = new char[47]; CR028:                fs.Read(byteBlockData,0,47);                       // Convert 'byte' to 'char'. CR029:                for(int mm = 0; mm < 47; mm++) CR030:                charBlockData[mm] = Convert.ToChar(byteBlockData[mm]); CR031:                string strBlockData = new string(charBlockData); CR032:                MessageBox.Show("BlockData read is '" + strBlockData + "' ."); CR033:              } CR034:              catch CR035:              { CR036:                MessageBox.Show("Cannot open file 'WeeklyNetWorth.dta'."); CR037:                return; CR038:              } CR039:            fs.Close(); CR040:            } //-----------------------------------------------------------------------------------------// CR050:            private void button2_Click(object sender, EventArgs e) CR051:            { // Begin File Opening and Reading With 'StreamReader'. CR052:              char[] charWNWBlock = new char[47]; CR053:              // Open existing file 'WeeklyNetWorth.dta'. CR054:              // The file must be located in 'CRAandWFiles\bin\debug'. CR055:              StreamReader qq = new StreamReader("WeeklyNetWorth.dta", Encoding.ASCII); CR056:              qq.ReadBlock(charWNWBlock, 0, 47); CR057:              string strWNWBlock = new string(charWNWBlock); CR058:              MessageBox.Show("Part Two: strWNWBlock = '" + strWNWBlock + "' ."); CR059:              qq.Close(); CR060:            } //-----------------------------------------------------------------------------------------// CR070:            private void button3_Click(object sender, EventArgs e) CR071:            { // Begin File Opening and Writing With 'StreamWriter'. CR072:              string strWNWBlock = "0444 04-44 22222.22 33333.33 444.44 ".ToString();                     // Open existing file 'ShortSpare.dta' and write to it.                     // 'true' below means 'append to file'. 'false' means overwrite. CR073:              StreamWriter jj = new StreamWriter("ShortSpare.dta",false,                                       Encoding.ASCII,47); CR074:              jj.Write(strWNWBlock); CR075:              MessageBox.Show("Part Three: strWNWBlock = '" + strWNWBlock + "' ."); CR076:              jj.Close(); CR077:            } 

When the Begin File Opening and Writing With StreamWriter button is clicked, the button3_Click event handler is actuated. A 47-char string is loaded into strWNWBlock so it can be written to a file named ShortSpare.dta. Line CR073 opens the file and prepares to write ASCII data to it. Line CR074 performs the write operation.

Button 4 is the standard Quit button.

 CR080:            private void button4_Click(object sender, EventArgs e) CR081:            { // Quit. CR082:              Close(); CR083:            } CR084:          } CR085:        } //=========================================================================================// Form1.Designer.cs: CR100:        namespace CRAndWFiles CR101:        { CR102:          partial class Form1 CR103:          {                   // Required designer variable. CR104:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. CR105:            protected override void Dispose(bool disposing) CR106:            { CR107:              if (disposing && (components != null)) components.Dispose(); CR108:              base.Dispose(disposing); CR109:            } CR110:            #region Windows Form Designer generated code                   // Required method for Designer support. CR111:            private void InitializeComponent() CR112:            { CR113:              this.button1 = new System.Windows.Forms.Button(); CR114:              this.button2 = new System.Windows.Forms.Button(); CR115:              this.button3 = new System.Windows.Forms.Button(); CR116:              this.button4 = new System.Windows.Forms.Button(); CR117:              this.SuspendLayout();                     //                     // button1                     // CR118:              this.button1.Location = new System.Drawing.Point(82, 15); CR119:              this.button1.Name = "button1"; CR120:              this.button1.Size = new System.Drawing.Size(225, 25); CR121:              this.button1.TabIndex = 0; CR122:              this.button1.Text = "Begin File Opening With \'FileStream\'"; CR123:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // CR124:              this.button2.Location = new System.Drawing.Point(44, 54); CR125:              this.button2.Name = "button2"; CR126:              this.button2.Size = new System.Drawing.Size(300, 25); CR127:              this.button2.TabIndex = 1; CR128:              this.button2.Text = "Begin File Opening and Reading with                       \'StreamReader\'"; CR129:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // button3                     // CR130:              this.button3.Location = new System.Drawing.Point(50, 94); CR131:              this.button3.Name = "button3"; CR132:              this.button3.Size = new System.Drawing.Size(300, 25); CR133:              this.button3.TabIndex = 2; CR134:              this.button3.Text = "Begin File Opening and Writing with                       \'StreamWriter\'"; CR135:              this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // button4                     // CR136:              this.button4.Location = new System.Drawing.Point(162, 133); CR137:              this.button4.Name = "button4"; CR138:              this.button4.Size = new System.Drawing.Size(75, 25); CR139:              this.button4.TabIndex = 3; CR140:              this.button4.Text = "Quit"; CR141:              this.button4.Click += new System.EventHandler(this.button4_Click);                     //                     // Form1                     // CR142:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); CR143:              this.ClientSize = new System.Drawing.Size(384, 172); CR144:              this.Controls.Add(this.button4); CR145:              this.Controls.Add(this.button3); CR146:              this.Controls.Add(this.button2); CR147:              this.Controls.Add(this.button1); CR148:              this.Name = "Form1"; CR149:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; CR150:              this.Text = "Create, Read, and Write Files"; CR151:              this.ResumeLayout(false); CR152:            } CR153:            #endregion CR154:            private System.Windows.Forms.Button button1; CR155:            private System.Windows.Forms.Button button2; CR156:            private System.Windows.Forms.Button button3; CR157:            private System.Windows.Forms.Button button4; CR158:          } 
 


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