public void SavePhonebookToFile(CallerList callerList)
        {
            var dialogResult = SaveFileDialog();

            if (dialogResult != DialogResult.OK)
            {
                return;
            }

            CreateFile(path);
            if (fileStream == null)
            {
                return;
            }

            using (StreamWriter streamWriter = new StreamWriter(fileStream))
            {
                for (int i = 0; i < callerList.Count; i++)
                {
                    Caller caller = callerList.GetAt(i);

                    streamWriter.WriteLine(caller.Record.Name + COLUMN_SEPARATOR + caller.Record.PhoneNumber);
                }
            }
        }
        public CallerList LoadPhonebookFromFile()
        {
            var dialogResult = OpenFileDialog();

            if (dialogResult != DialogResult.OK)
            {
                return(null);
            }

            fileStream = File.OpenRead(path);

            var callerList = new CallerList();

            using (StreamReader streamReader = new StreamReader(fileStream))
            {
                while (!streamReader.EndOfStream)
                {
                    string line = streamReader.ReadLine();

                    if (!string.IsNullOrEmpty(line))
                    {
                        string[] columns = line.Split(new string[] { COLUMN_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);

                        if (columns.Length == 2)
                        {
                            callerList.Add(new Caller(new Record(columns[0], columns[1])));
                        }
                    }
                }
            }

            return(callerList);
        }
Exemple #3
0
        public void LoadPhonebookFromFile()
        {
            var loadedCallerList = fileStream.LoadPhonebookFromFile();

            if (loadedCallerList != null)
            {
                callerList = loadedCallerList;
            }
        }