WinForm에서 이진 파일을 읽고 쓰려고합니다. 내가 해냈다 고 생각했지만 파일을 읽으려고 할 때 파일에 기록 된 새 번호 만 가져옵니다 (5 초마다 임의의 숫자가 생성됨). 그 파일은 이전 수치를 유지하지 않습니다. 내가 한 일 :왜 바이너리 파일을 쓸 수 없습니까?
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);
}
내가 뭘 잘못하고 있니? 나를 도울 수있는 사람이 있습니까? 미리 감사드립니다.
왜 플러시 방법입니까? – Steve
동일한 결과가 나타납니다. 끊임없이 최신 번호. – Steve
@Steve 원래 코드에 있기 때문에. 방금 불필요한'if' 블록을 제거했습니다. –