How to use Filestream class?

I wrote a program that does exactly what it's supposed to: read a .csv file, edit it and then save the changes to the .csv file. Unfortunately, my program has to include the filestream class (yes this is homework). Can anyone give me a pointer for how to include it into my code? Or point me towards a good resource?

Here's my code:

public partial class Form1 : Form { //adds required elements DataGridView my_datagridview = new DataGridView(); DataTable my_datatable = new DataTable(); Button export_button = new Button(); //This textbox isn't visible to the user. It is only to temporarily store and allow for formatting of the stocklist file. TextBox text_box_export = new TextBox(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Sets window sizes this.Size = new Size(600, 450); my_datagridview.Size = new Size(450, 400); my_datagridview.Location = new Point(5, 5); text_box_export.Multiline = true; export_button.Size = new Size(100, 50); export_button.Location = new Point(470, 5); export_button.Text = "Export data"; //makes datagrid & button visible this.Controls.Add(export_button); this.Controls.Add(my_datagridview); //binds data source to datagrid my_datagridview.DataSource = my_datatable; //button click event export_button.Click += new EventHandler(export_button_Click); //opens .csv file into string array string[] raw_text = System.IO.File.ReadAllLines("c:\\StockFile\\stocklist2.csv"); //splits data into cells by looking for commas string[] data_col = null; int x = 0; foreach (string text_line in raw_text) { data_col = text_line.Split(','); if (x == 0) { //to determine header for (int i = 0; i <= data_col.Count() - 1; i++) { my_datatable.Columns.Add(data_col[i]); } x++; } else { //to determine data my_datatable.Rows.Add(data_col); } } } //'Export data' button void export_button_Click(object sender, EventArgs e) { //clears form and ensures new file will have header text_box_export.Text = "Item Code,Item Description,Current Count,On Order\r\n"; //ints for for loops int row_count = my_datagridview.RowCount; int cell_count = my_datagridview.Rows[0].Cells.Count; //captures data from each row for (int row_index = 0; row_index <= row_count-2; row_index++) { //captures data from each column in each row for (int cell_index = 0; cell_index < cell_count; cell_index++) { //adds data to messagebox in stocklist format & leaves comma off the end of each line if (cell_index <=2) { text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString() + ","; } else { text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString(); } } text_box_export.Text = text_box_export.Text + "\r\n"; } //writes new data over stocklist.csv file System.IO.File.WriteAllText("c:\\Stockfile\\stocklist2.csv", text_box_export.Text); } } 

And a sample of the .csv file:

Item Code,Item Description,Current Count,On Order A0040,Petrol Station,4,Yes A0039,Traffic Lights,5,Yes A0038,Shop,7,No A0037,Building (tall),4,No 

by Mopstrr via /r/csharp

Leave a Reply