コード例 #1
0
        private void btn_IncarcareJoc_Click(object sender, EventArgs e)
        {
            int            ok       = 0;
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.Filter = "Text files (*.txt)|*.txt";
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    string     path      = openFile.FileName;
                    Stream     stream    = new FileStream(path, FileMode.Open, FileAccess.Read);
                    IFormatter formatter = new BinaryFormatter();
                    date1 = (DateCastigPartida)formatter.Deserialize(stream);

                    int i = 0;
                    while (stream.Position != stream.Length)
                    {
                        date2[i] = (DateleNouluiJoc)formatter.Deserialize(stream);
                        i++;
                    }
                    stream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fișier invalid");
                    ok = 1;
                }
                if (ok == 0)
                {
                    this.Hide();
                    MainGame jocnou = new MainGame();
                    jocnou.SetValue(1);
                    jocnou.ShowDialog();
                }
            }
        }
コード例 #2
0
ファイル: ExportGame.cs プロジェクト: BogdanFi/Licenta
 private void button1_Click(object sender, EventArgs e)
 {
     if (ok == true)
     {
         SaveFileDialog sfd = new SaveFileDialog();
         sfd.Filter   = "Text files (*.txt)|*.txt";
         sfd.FileName = "GameRules";
         if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             try
             {
                 ok           = false;
                 button1.Text = "Start game";
                 string     path      = sfd.FileName;
                 IFormatter formatter = new BinaryFormatter();
                 Stream     stream    = new FileStream(path, FileMode.Create, FileAccess.Write);
                 formatter.Serialize(stream, date1);
                 for (int i = 0; i < date2.Length; i++)
                 {
                     formatter.Serialize(stream, date2[i]);
                 }
                 stream.Close();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
         }
     }
     else
     {
         this.Close();
         MainGame jocnou = new MainGame();
         jocnou.SetValue(2);
         jocnou.ShowDialog();
     }
 }