Monday, 16 September 2013

File Handling

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;



namespace file_handling_cs
{
    public partial class Form1 : Form
    {
       
        StreamWriter sw;
        FileStream fs;

        public void fun()
        {          
            sw.WriteLine(textBox1.Text);
            sw.WriteLine(textBox2.Text);
            if (opt1.Checked == true)
            {
                sw.WriteLine(opt1.Text);
            }
            else
            {
                sw.WriteLine(opt2.Text);
            }
            sw.WriteLine(comboBox1.Text);
            sw.WriteLine(textBox3.Text);
            sw.WriteLine(textBox4.Text);
            sw.WriteLine(comboBox2.Text);
       
            sw.Close();
            fs.Close();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string z = openFileDialog1.FileName;
            System.IO.FileStream fs = new System.IO.FileStream(z, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs);
            textBox1.Text = sr.ReadLine();
            textBox2.Text = sr.ReadLine(); // it will read one line at a time
            //sr.ReadToEnd();                                        //it will read complete file at a time
            sr.Close();
            fs.Close();
        }  
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();                                                                  
            string z = saveFileDialog1.FileName;
            fs = new System.IO.FileStream(z, System.IO.FileMode.Create );
            sw = new System.IO.StreamWriter(fs);
            fun();
            MessageBox.Show("saved");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
                                                                 
            string z = saveFileDialog1.FileName;
            fs = new System.IO.FileStream(z, System.IO.FileMode.Append);
           sw = new System.IO.StreamWriter(fs);
            fun();
       }
        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();                                                                
            string z = openFileDialog1.FileName;
            System.IO.FileStream fs = new System.IO.FileStream(z, System.IO.FileMode.Truncate);
       }

     
    }
}



No comments:

Post a Comment