Exemplo n.º 1
0
        //Hier wird das File beim aufruf der Klasse gelesen
        public Highscore()
        {
            removeFileSave();
            String line = null; //save the line from file
            if (!File.Exists(this.getPath()))
            {
                string[] lines = new string[21];
                Console.WriteLine("Highscore-Datei nicht gefunden. Es wird eine Neue erstellt");
                //File.Create(@currentPath + "\\highscore.txt");
                lines[0] = "|Platz|Name|Punkte|";
                for (int i = 0; i < 20; i++)
                {
                    lines[(i + 1)] = "|" + (i + 1) + "|-------|0|";
                }
                File.WriteAllLines(this.getPath(), lines);
            }

            StreamReader sr = new StreamReader(this.getPath()); //open the txt-file at path

            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                string[] splitparts = line.Split('|');

                foreach (char part in line)
                {
                    if (part.Equals("") || part.Equals(" "))
                    {
                        break;
                    }
                }

                if (header == null)
                {
                    header = new string[splitparts.Length];
                    for (int i = 0; i < splitparts.Length - 1; i++)
                    {
                        header[i] = splitparts[i].Trim();
                    }
                    continue;
                }

                if (splitparts.Length != header.Length)
                {
                    Console.WriteLine("Anzahl der Spalten stimmt nicht!");
                    continue;
                }

                PlayerHolder playerHolder = new PlayerHolder();

                for (int i = 1; i < splitparts.Length - 1; i++)
                {
                    if (splitparts[i].Equals(""))
                    {
                        splitparts[i] = null;
                    }
                    playerHolder.setAttribut(header[i], splitparts[i].Trim());
                }
                playerHolders.Add(playerHolder);
            }
            sr.Close();
            setFileSave();
        }
Exemplo n.º 2
0
        //Hier wird das File beim aufruf der Klasse gelesen
        public Highscore()
        {
            removeFileSave();
            String line = null; //save the line from file

            if (!File.Exists(this.getPath()))
            {
                string[] lines = new string[21];
                Console.WriteLine("Highscore-Datei nicht gefunden. Es wird eine Neue erstellt");
                //File.Create(@currentPath + "\\highscore.txt");
                lines[0] = "|Platz|Name|Punkte|";
                for (int i = 0; i < 20; i++)
                {
                    lines[(i + 1)] = "|" + (i + 1) + "|-------|0|";
                }
                File.WriteAllLines(this.getPath(), lines);
            }

            StreamReader sr = new StreamReader(this.getPath()); //open the txt-file at path

            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                string[] splitparts = line.Split('|');

                foreach (char part in line)
                {
                    if (part.Equals("") || part.Equals(" "))
                    {
                        break;
                    }
                }

                if (header == null)
                {
                    header = new string[splitparts.Length];
                    for (int i = 0; i < splitparts.Length - 1; i++)
                    {
                        header[i] = splitparts[i].Trim();
                    }
                    continue;
                }

                if (splitparts.Length != header.Length)
                {
                    Console.WriteLine("Anzahl der Spalten stimmt nicht!");
                    continue;
                }

                PlayerHolder playerHolder = new PlayerHolder();

                for (int i = 1; i < splitparts.Length - 1; i++)
                {
                    if (splitparts[i].Equals(""))
                    {
                        splitparts[i] = null;
                    }
                    playerHolder.setAttribut(header[i], splitparts[i].Trim());
                }
                playerHolders.Add(playerHolder);
            }
            sr.Close();
            setFileSave();
        }