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

                if (result == true)
                {
                    string tiedostonimi = sfd.FileName;
                    string tyyppi       = System.IO.Path.GetExtension(tiedostonimi);
                    if (tyyppi == ".txt")
                    {
                        try
                        {
                            StreamWriter sw   = new StreamWriter(tiedostonimi);
                            string       rivi = "";
                            foreach (var arvo in pelaajaLista)
                            {
                                rivi = arvo.GetEverything();
                                sw.WriteLine(rivi);
                            }
                            sw.Close();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    if (tyyppi == ".xml")
                    {
                        try
                        {
                            Pelaaja.SerialisoiXml(tiedostonimi, pelaajaLista);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }