コード例 #1
0
        private void btnLoadResults_Click(object sender, EventArgs e)
        {
            //File IO could be moved to File Service and called here
            var file = new FileService.FileIO();

            //Could display message if file does not exist
            //The form could have a file selector but I was trying to keep things simple
            if (File.Exists(txtFileName.Text))
            {
                txtHighScores.Text = string.Empty;
                var split = File.ReadLines(txtFileName.Text);

                //The data has been store as text lines with a separator of " - "
                if (split.Count() > 0)
                {
                    HighScores.Clear();
                    foreach (var line in split)
                    {
                        txtHighScores.Text += line + System.Environment.NewLine;
                        var splitLine = line.Replace(" ", "").Split('-');

                        HighScores.Add(new HighScoreViewModel()
                        {
                            Score = int.Parse(splitLine[1]), UserName = splitLine[0], ElapsedTimeInMilliseconds = long.Parse(splitLine[2])
                        });
                    }
                }
            }
        }
コード例 #2
0
        private void btnSaveResults_Click(object sender, EventArgs e)
        {
            if (txtFileName.Text == string.Empty)
            {
                MessageBox.Show("A file name is required!");
                return;
            }

            var file = new FileService.FileIO();

            file.WriteToTextFile(txtFileName.Text, txtHighScores.Text);
        }