reading & writing a text file   Leave a comment


                                                                                                                                                                                        

using System; 

using System.Windows.Forms; 

using System.IO; 

namespace prash_textpad 

    public partial class prash_textpad : Form 

    { 

        public prash_textpad() 

        { 

            InitializeComponent(); 

        } 

        private void open_btn_Click(object sender, EventArgs e) 

        { 

            openFileDialog1.Filter = “text|*.txt|documents|*.doc|AllFiles|*.*”; 

            if (openFileDialog1.ShowDialog() == DialogResult.OK) 

            { 

                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); 

                StreamReader sr = new StreamReader(fs); 

                string s, str = null; 

                s = sr.ReadLine(); 

                while (s != null) 

                { 

                    str = str + s + Convert.ToChar(13) + Convert.ToChar(10); 

                    s = sr.ReadLine(); 

                } 

                textBox1.Text = str; 

                sr.Close(); 

                fs.Close(); 

            } 

        } 

        private void save_btn_Click(object sender, EventArgs e) 

        { 

            saveFileDialog1.Filter = “text|*.txt|documents|*.doc|AllFiles|*.*”; 

            if (saveFileDialog1.ShowDialog() == DialogResult.OK) 

            { 

                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite); 

                StreamWriter sw = new StreamWriter(fs); 

                sw.WriteLine(textBox1.Text); 

                sw.Close(); 

                fs.Close(); 

            } 

        } 

        private void clear_btn_Click(object sender, EventArgs e) 

        { 

            textBox1.Clear();                                                                                                               

        } 

        private void exit_btn_Click(object sender, EventArgs e) 

        { 

            this.Close(); 

        }  

    } 

}

Advertisements

Posted September 2, 2010 by PrashanthChindam in c#

Tagged with

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: