locked
save and open text file simple program RRS feed

  • Question

  • 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;

    namespace WindowsFormsApplication1
    {
        public partial class form1 : Form
        {
            public form1()
            {
                InitializeComponent();
            }
            public class samochod
            {
                public string marka;
                public string model;
                public string rokProdukcji;
                // Method
                public void Dodaj(string Marka,string Model,string RokProdukcji)
                {
                    marka = Marka;
                    model = Model;
                    rokProdukcji = RokProdukcji;
                }


            }
            string path = "C:\\Documents and Settings\\student\\Pulpit\\plik.txt";
            samochod samochod1 = new samochod();
            List<string> samochody = new List<string>();
            private void button1_Click(object sender, EventArgs e)//dodawanie(add)
            {
                string samo = "";
                samochod1.Dodaj(tb1.Text, tb2.Text, tb3.Text);
                samo = samochod1.marka + " " + samochod1.model + " " + samochod1.rokProdukcji;
                listBox1.Items.Add(samo);
                samochody.Add(samo);

            }

            private void button2_Click(object sender, EventArgs e)//pokaz(look)
            {
                listBox1.Items.Clear();
                int ilosc=samochody.Count;
                for (int i = 0; i < ilosc; i++)
                {
                    listBox1.Items.Add(samochody[i]);
                }            
            }

            private void button3_Click(object sender, EventArgs e)//usun(delete)
            {
                samochody.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Clear();
                int ilosc = samochody.Count;
                for (int i = 0; i < ilosc; i++)
                {
                    listBox1.Items.Add(samochody[i]);
                }
            }

            private void button4_Click(object sender, EventArgs e)//zapis do pliku(save to file)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@path))
                {
                    foreach (string line in samochody)
                    {
                        // If the line doesn't contain the word 'Second', write the line to the file.
                        if (!line.Contains("Second"))
                        {
                            file.WriteLine(line);
                        }
                    }
                }
            }

            private void button5_Click(object sender, EventArgs e)//odczyt z pliku(open to file)
            {
                string[] lines = System.IO.File.ReadAllLines(@path);
                foreach (string line in lines)
                {
                    listBox1.Items.Add(line);
                    samochody.Add(line);
                }
            }

            private void button6_Click(object sender, EventArgs e)//(search)
            {
                listBox1.Items.Clear();
                string szukaj = tb4.Text;
                int ilosc = samochody.Count;
                int l = 0;
                for (int i = 0; i < ilosc; i++)
                {
                    if (samochody[i].Contains(szukaj))
                        {
                            listBox1.Items.Add(samochody[i]);
                        }

                }

            }
    Monday, September 21, 2015 7:59 PM

Answers

  • As far as I understand you have a in memory list<string> (samodchi) that you work on via WinForms GUI elements (ComboBox, buttons).
    You also want to read and write the data to a file.

    Things I noticed:

    You hardcode the path to the file in teh user profile. It is okay for a test, but for actuall code you should retrieve the right path from Windows: https://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

    You open the StreamWriter in override mode. Your properly dispose of the isntance afterwards.

    If this is going to be international, you might want to take care to clearly define one encoding to be used during read and write.

    I honestly can not find any mistakes in this code.

    • Proposed as answer by Kristin Xie Wednesday, September 23, 2015 6:25 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 2:39 AM
    Monday, September 21, 2015 8:29 PM

All replies

  • Your forgot to ask any question or tell us what the problem is.
    You also forgot to put that code into a code block so it is easily readable:

    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;
    
    namespace WindowsFormsApplication1
    {
        public partial class form1 : Form
        {
            public form1()
            {
                InitializeComponent();
            }
            public class samochod
            {
                public string marka;
                public string model;
                public string rokProdukcji;
                // Method
                public void Dodaj(string Marka,string Model,string RokProdukcji)
                {
                    marka = Marka;
                    model = Model;
                    rokProdukcji = RokProdukcji;
                }
    
    
            }
            string path = "C:\\Documents and Settings\\student\\Pulpit\\plik.txt";
            samochod samochod1 = new samochod();
            List<string> samochody = new List<string>();
            private void button1_Click(object sender, EventArgs e)//dodawanie(add)
            {
                string samo = "";
                samochod1.Dodaj(tb1.Text, tb2.Text, tb3.Text);
                samo = samochod1.marka + " " + samochod1.model + " " + samochod1.rokProdukcji;
                listBox1.Items.Add(samo);
                samochody.Add(samo);
    
            }
    
            private void button2_Click(object sender, EventArgs e)//pokaz(look)
            {
                listBox1.Items.Clear();
                int ilosc=samochody.Count;
                for (int i = 0; i < ilosc; i++)
                {
                    listBox1.Items.Add(samochody[i]);
                }            
            }
    
            private void button3_Click(object sender, EventArgs e)//usun(delete)
            {
                samochody.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Clear();
                int ilosc = samochody.Count;
                for (int i = 0; i < ilosc; i++)
                {
                    listBox1.Items.Add(samochody[i]);
                }
            }
    
            private void button4_Click(object sender, EventArgs e)//zapis do pliku(save to file)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@path))
                {
                    foreach (string line in samochody)
                    {
                        // If the line doesn't contain the word 'Second', write the line to the file.
                        if (!line.Contains("Second"))
                        {
                            file.WriteLine(line);
                        }
                    }
                }
            }
    
            private void button5_Click(object sender, EventArgs e)//odczyt z pliku(open to file)
            {
                string[] lines = System.IO.File.ReadAllLines(@path);
                foreach (string line in lines)
                {
                    listBox1.Items.Add(line);
                    samochody.Add(line);
                }
            }
    
            private void button6_Click(object sender, EventArgs e)//(search)
            {
                listBox1.Items.Clear();
                string szukaj = tb4.Text;
                int ilosc = samochody.Count;
                int l = 0;
                for (int i = 0; i < ilosc; i++)
                {
                    if (samochody[i].Contains(szukaj))
                        {
                            listBox1.Items.Add(samochody[i]);
                        }
    
                }
    
            }

    Monday, September 21, 2015 8:15 PM
  • Hi,

    Please ask a question.

    Rgds MM


    PS: Please mark as answer if helpful. Thanks!
    Blog: http://www.manuelmeyer.net
    Twitter: https://twitter.com/manumeyer1

    Monday, September 21, 2015 8:18 PM
  • As far as I understand you have a in memory list<string> (samodchi) that you work on via WinForms GUI elements (ComboBox, buttons).
    You also want to read and write the data to a file.

    Things I noticed:

    You hardcode the path to the file in teh user profile. It is okay for a test, but for actuall code you should retrieve the right path from Windows: https://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

    You open the StreamWriter in override mode. Your properly dispose of the isntance afterwards.

    If this is going to be international, you might want to take care to clearly define one encoding to be used during read and write.

    I honestly can not find any mistakes in this code.

    • Proposed as answer by Kristin Xie Wednesday, September 23, 2015 6:25 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 2:39 AM
    Monday, September 21, 2015 8:29 PM