Exemplo n.º 1
0
        private void Open(int mode) //Avaa File Explorerin, jonka avulla ladataan tiedostoja
        {
            //mode: 1 = lataa kaikki, 2 = lataa seulaAlueen tiedot, 3 = lataa ohjealueet, 4 = lataa tekstitiedot
            Stream         stream         = null;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Multiselect      = false;
            openFileDialog.Filter           = "Tallennustiedosto (JSON) (*.json)|*.json";
            openFileDialog.Title            = "Avaa tallennus";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    if ((stream = openFileDialog.OpenFile()) != null)
                    {
                        using (stream)
                        {
                            StreamReader sr   = new StreamReader(stream);
                            string       json = sr.ReadToEnd();
                            SaveLoadFunc load = new SaveLoadFunc();
                            load.LoadAll(this, json, mode);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Tiedoston avaaminen epäonnistui" + ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        private void Save()
        {
            //Avataan tallennusdialogi ja tallennetaan tiedosto haluamaan sijaintiin
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter           = "Tallennustiedosto (JSON) (*.json)|*.json";
            saveFileDialog.Title            = "Tallenna nykyinen sessio";
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            SaveLoadFunc sa   = new SaveLoadFunc();
            string       json = sa.Save(this, seulalista);

            if (saveFileDialog.ShowDialog() == true)
            {
                if (saveFileDialog.FileName != String.Empty || saveFileDialog.FileName != "")
                {
                    //FileStream fs = (FileStream)saveFileDialog.OpenFile();
                    StreamWriter s = new StreamWriter(saveFileDialog.FileName);
                    s.WriteLine(json);
                    //fs.Close();
                    s.Close();
                }
            }
        }