public void Start()
{
List<Object> list = new List<Object>();
List<Object> toRemove = new List<Object>();
if (File.Exists("c.bin"))
{
using (Stream osR = File.OpenRead("c.bin"))
{
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
try
{
using (CryptoStream crStream = new CryptoStream(osR, cryptic.CreateDecryptor(), CryptoStreamMode.Read))
{
if (osR.Length > 0)
{
byte[] data = ASCIIEncoding.ASCII.GetBytes(osR.ToString());
crStream.Read(data, 0, data.Length);
BinaryFormatter formatter = new BinaryFormatter();
Object obj = formatter.Deserialize(crStream);
toRemove.Add(obj);
}
}
}
catch (CryptographicException e) { Console.WriteLine("error" + e.Message); }
}
Console.WriteLine("------------------");
if (File.Exists("d.bin"))
{
using (Stream os = File.OpenRead("d.bin"))
{
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
try
{
using (CryptoStream crStream = new CryptoStream(os, cryptic.CreateDecryptor(), CryptoStreamMode.Read))
{
if (os.Length > 0)
{
byte[] data = ASCIIEncoding.ASCII.GetBytes(os.ToString());
crStream.Read(data, 0, data.Length);
BinaryFormatter formatter = new BinaryFormatter();
Object obj = formatter.Deserialize(crStream);
if(!isContained(obj,toRemove)
list.Add(obj);
}
}
}
catch (CryptographicException e) { Console.WriteLine("error" + e.Message); }
}
}
if (!File.Exists("d.bin"))
stream = File.Create("d.bin");
foreach (Object obj in list)
{
if (obj is Product)
AddProduct((Product)obj);
else
{
if (obj is Department)
AddDepartment((Department)obj);
else
{
if (obj is Employee)
AddEmployee((Employee)obj);
else
{
if (obj is ClubMember)
AddClubMember((ClubMember)obj);
else
{
if (obj is Transaction)
AddTransaction((Transaction)obj);
else
{
if (obj is User)
AddUser((User)obj);
}
}
}
}
}
}
if(!File.Exists("c.bin")
removeStream = File.Create("c.bin");
}
public void End()
{
stream.Close();
removeStream.Close();
}
public void AddProduct(Backend.Product p)
{
productDB.Add(p);
using (stream)
{
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
using (CryptoStream crStream = new CryptoStream(stream, cryptic.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] data = ASCIIEncoding.ASCII.GetBytes(stream.ToString());
crStream.Write(data, 0, data.Length);
BinaryFormatter seri = new BinaryFormatter();
seri.Serialize(crStream, p);
}
}
}
}
i have the Start() function which initializes the program with previous data(which was inserted in previous uses in the program).
while running the program,the user can add things,such as products-then the AddProduct function is called and serializes it file.
my problem is that in the above code i get an error-"stream was not writable".
if i change the using line to-using(stream=File.OpenWrite("d.bin")) it's working but it overwrites the data already in the file and the next time i open the program i only see the last added object.
any suggestions on how to solve it or how change the code so this situation won't happen?