Wersja robocza . Chętnie wysłucham krytycznych uwag !
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static string domyslnyKatalog = Directory.GetCurrentDirectory();
static void NadajSpecyfikacje(Komputer komp)
{
Console.Write("Podaj nazwe komputera: ");
komp.Nazwa = Console.ReadLine();
Console.Write("Podaj procesor komputera: ");
komp.Procesor = Console.ReadLine();
Console.Write("Podaj karte graficzną komputera: ");
komp.KartaGraficzna = Console.ReadLine();
}
static void PokazSpecyfikacje(Komputer komp)
{
Console.Write("Nazwa komputera: "); Console.WriteLine(komp.Nazwa);
Console.Write("Procesor: "); Console.WriteLine(komp.Procesor);
Console.Write("Karta graficzna: "); Console.WriteLine(komp.KartaGraficzna);
Console.ReadKey();
}
static void ZapiszListe(List<Komputer> lista)
{
Console.Write("Podaj nazwę pliku : ");
string nazwaPliku = string.Format("{0}.dat", Console.ReadLine());
BinaryFormatter binFormat = new BinaryFormatter();
try
{
using (Stream fStream = new FileStream(Path.Combine(domyslnyKatalog, nazwaPliku), FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
binFormat.Serialize(fStream, lista);
}
}
catch(ArgumentException)
{
Console.WriteLine("Nieprawidłowa nazwa pliku!");
Console.ReadKey();
}
}
static List<Komputer> WczytajListe()
{
Console.WriteLine("Dostępne pliki :");
foreach (string elm in Directory.GetFiles(domyslnyKatalog, "*.dat"))
{
Console.WriteLine(" - " + Path.GetFileNameWithoutExtension(elm));
}
Console.Write("Podaj nazwę pliku : ");
string nazwaPliku = string.Format("{0}.dat", Console.ReadLine());
BinaryFormatter binFormat = new BinaryFormatter();
try
{
using (Stream fStream = File.OpenRead(Path.Combine(domyslnyKatalog, nazwaPliku)))
{
List<Komputer> listaKomputerow = (List<Komputer>)binFormat.Deserialize(fStream);
return listaKomputerow;
}
}
catch(FileNotFoundException e)
{
Console.WriteLine(e.Message);
return null;
}
catch(SerializationException )
{
Console.WriteLine("Nieprawidłowy plik!");
Console.ReadKey();
return null;
}
}
static void ZmienFolder()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(domyslnyKatalog);
Console.ResetColor();
Console.Write("Nowy: ");
string nowaSciezka = Console.ReadLine();
if (Directory.Exists(nowaSciezka)) domyslnyKatalog = nowaSciezka;
else
{
Console.WriteLine("Podanay katalog nie istnieje!");
Console.ReadKey();
}
}
static void Main(string[] args)
{
bool koniec = true;
int wybor;
Komputer komp;
List<Komputer> listaKomputerow = new List<Komputer>();
do
{
Console.Clear();
Console.WriteLine("Witaj w menu głównym! - liczba komputerów {0}", listaKomputerow.Count);
Console.WriteLine("[1] Nowy komputer");
Console.WriteLine("[2] Pokaż specyfikacje");
Console.WriteLine("[3] Wyjdź z programu");
Console.WriteLine("[4] Zapisz");
Console.WriteLine("[5] Wczytaj");
Console.WriteLine("[6] Zmień folder zapisu/odczytu");
Console.WriteLine("[7] Wyczyść listę");
Console.Write("Twój wybór : ");
if (int.TryParse(Console.ReadLine(), out wybor))
{
switch (wybor)
{
case 1:
komp = new Komputer();
NadajSpecyfikacje(komp);
int indeks = listaKomputerow.FindIndex(k => komp.Nazwa == k.Nazwa);
if (indeks == -1)
{
listaKomputerow.Add(komp);
}
else
{
listaKomputerow[indeks] = komp;
}
break;
case 2:
Console.Write("Podaj nazwę komputera : ");
string nazwa = Console.ReadLine();
Komputer szukanyKomputer = listaKomputerow.Find(k => nazwa == k.Nazwa);
if (szukanyKomputer != null) PokazSpecyfikacje(szukanyKomputer);
else
{
Console.WriteLine("Komputer o podanej nazwie nie istnieje!");
Console.ReadKey();
}
break;
case 3:
koniec = false;
break;
case 4:; ZapiszListe(listaKomputerow);
break;
case 5:
List<Komputer> nowaLista = WczytajListe();
if (nowaLista != null) listaKomputerow = nowaLista;
break;
case 6: ZmienFolder();
break;
case 7:
listaKomputerow.Clear();
break;
}
}
} while (koniec);
}
}
[Serializable]
class Komputer
{
public string Nazwa { get; set; }
public string Procesor { get; set; }
public string KartaGraficzna { get; set; }
}
}