I am trying to write and read binary file in a WinForm. I thought that I did it but when I try to read the file, I only get the new number that was written to the file (random numbers generated every 5 seconds), that file does not retain the previous figures. what i did:
private void timer1_Tick(object sender, EventArgs e)
{
string path = @"C:\Test\test.dat";
lbl1.Text = string.Format("{0:0.0}", -6 + rand.NextDouble() * 17);
double temp = Convert.ToDouble(lbl1.Text);
try
{
if (!File.Exists(path))
{
lock (sync)
{
FileStream outf = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(outf);
bw.Write(temp);
bw.Close();
}
}
else if (File.Exists(path))
{
lock (synk)
{
FileStream outf1 = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw1 = new BinaryWriter(outf1);
bw1.Write(temp);
bw1.Flush();
bw1.Close();
}
}
}
catch (System.IO.FileNotFoundException ioe)
{
MessageBox.Show(ioe.Message);
}
What am I doing wrong? Is there anyone who can help me? Thanks in advance.