Example #1
0
        private void btnLue_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = "d:\\";
                ofd.Filter           = "Tekstitiedostot .txt|*.txt|Xml-tiedostot .xml|*.xml";
                Nullable <bool> result = ofd.ShowDialog();

                if (result == true)
                {
                    string tiedostonimi = ofd.FileName;
                    string tyyppi       = System.IO.Path.GetExtension(tiedostonimi);

                    if (tyyppi == ".txt")
                    {
                        if (File.Exists(tiedostonimi))
                        {
                            pelaajaLista = new List <Pelaaja>();
                            StreamReader sr;
                            string       line;
                            try
                            {
                                sr = new StreamReader(File.OpenRead(tiedostonimi));
                                while (!sr.EndOfStream)
                                {
                                    line = sr.ReadLine();
                                    string[] tiedot = line.Split(' ');
                                    pelaajaLista.Add(new Pelaaja(tiedot[0], tiedot[1], int.Parse(tiedot[2]), tiedot[3]));
                                }
                                ApplyChanges();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                    if (tyyppi == ".xml")
                    {
                        if (File.Exists(tiedostonimi))
                        {
                            try
                            {
                                pelaajaLista = Pelaaja.DeSerialisoiXml(tiedostonimi);
                                ApplyChanges();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }