private void btnOpenSave_Click(object sender, RoutedEventArgs e)
        {
            // Creating \Pentegames directory so there is no error
            System.IO.Directory.CreateDirectory(@"\CardGameGallery\Go Fish");
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = @"C:\CardGameGallery\Go Fish";
            openFileDialog.Multiselect      = true;
            openFileDialog.Filter           = "Go Fish Saves|*.GoFish";
            //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    // Get the file path chosen by the user
                    stream = new FileStream(@$ "{openFileDialog.FileName}", FileMode.Open, FileAccess.Read);
                    // Deserializing the game save file to a GameSave object
                    save = (GoFishSaveGame)formatter.Deserialize(stream);
                    // Opening the play window with the GameSave object
                    playGoFishWindow = new PlayGoFishWindow(save, openFileDialog.FileName, this);
                    // Hiding main menu withle play window is up
                    Hide();
                    // Displaying the play window
                    playGoFishWindow.Show();
                    // CLosing the stream to free up resources
                    stream.Close();
                }
                catch
                {
                    // If an error occurs while opening save file
                    MessageBox.Show("Something went wrong, try again.");
                }
            }
        }
Example #2
0
        // Saves the current state of the game
        public static bool SaveGame(GoFishSaveGame save, string saveGamePath)
        {
            try
            {
                // Save files name is the current time
                string time = DateTime.Now.ToString("T");

                time = time.Replace(':', '-');

                IFormatter formatter = new BinaryFormatter();
                System.IO.Directory.CreateDirectory(@"\CardGameGallery\Go Fish");
                string path   = saveGamePath.Equals("") ? @$ "\CardGameGallery\Go Fish\{time}.GoFish" : saveGamePath;
                Stream stream = new FileStream(@$ "{path}", FileMode.Create, FileAccess.Write);

                formatter.Serialize(stream, save);
                stream.Close();
                System.Diagnostics.Debug.WriteLine("Save successful!");
                return(true);
            }