/*_________________________________________________________________________________ S T R E A M R E A D E R / W R I T E R D E M O Created 11/9/06 Ron Kessler C# 2005 Version _________________________________________________________________________________ Last updated 1/18/07 FEATURES: 1. Shows how to read/write to a sequential text file 2. Lets user create file when program first loads 3. Items can be added/removed from list box 4. Items in list box can be saved to disk 5. Shows how to use Foreach loop with list box items 6. Uses structured error handling * 7. I create Stream objects with the new "using" statement borrowed from VB. This creates * the object but when it goes out of scope, it automatically disposes of it!! */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //---be sure to add this for Stream Classes using System.IO; namespace Using_Streams { public partial class frmMain : Form { public frmMain () { InitializeComponent(); } //---class-level variables string diskFile = "..\\..\\inventory.txt"; //our datafile remember to include the ..\\ bit! private void frmMain_Load(object sender, EventArgs e) { DoReload(); } /// /// This method is called from form_load and reload_click to populate the listbox /// private void DoReload() { //---if file does not exist lets create it for them if they want to. if (File.Exists(diskFile) == false) { DialogResult button1 = MessageBox.Show( "Cannot locate inventory file. Shall I create it?","System Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (button1 == DialogResult.Yes) File.Create(diskFile); else this.Close(); //close the form } else { try { lstItems.Items.Clear(); //no duplicates using (StreamReader myReader = new StreamReader (diskFile)) //this will close/dispose of reader automatically { while (myReader.Peek() != -1) { string dataFromFile = myReader.ReadLine(); lstItems.Items.Add(dataFromFile); } myReader.Close(); } } catch (Exception e) { MessageBox.Show("An error occurred while reading...." + e.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } } } private void btnReload_Click(object sender, EventArgs e) { DoReload(); } /// /// Add new item to listbox /// /// /// private void btnAdd_Click(object sender, EventArgs e) { string myNewItem = txtNewItem.Text.Trim(); lstItems.Items.Add(myNewItem); } /// /// Update changes to text file /// /// /// private void btnSave_Click(object sender, EventArgs e) { try { using (StreamWriter myWriter = new StreamWriter(diskFile, false)) foreach (string myItem in lstItems.Items) { myWriter.WriteLine(myItem); } MessageBox.Show("Your data has been updated.", "System message",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("I could not save your changes." + ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// Clear the selected items in list box /// /// /// private void btnClear_Click (object sender, EventArgs e) { lstItems.Items.Clear(); } /// /// Let them confirm a deletion /// /// /// private void btnDelete_Click (object sender, EventArgs e) { DialogResult button = MessageBox.Show( "Are you sure you want to delete " + lstItems.SelectedItem + "?", "System Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (button == DialogResult.Yes) lstItems.Items.Remove(lstItems.SelectedItem); else txtNewItem.Focus(); } private void btnExit_Click (object sender, EventArgs e) { this.Close(); } } }