如何连续将数据写入文本文件?

时间:2014-12-01 12:36:03

标签: c# datetime streamwriter

我只想将DateTime(HH:mm:ss)和01写成文本文件中的一行。

如果收到的数据不包含0UCAST,则会显示

=g,如果是,则我想写DateTime1 。这是收到邮件的DateTime

我的问题是我只获得了DateTIme1,但却从未获得DateTime0。该行始终由新DateTime1替换,因此旧版本不再存在。

private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        recievdata = SerialPort.ReadExisting();
        this.Invoke(new EventHandler(DisplayText));

        using (Stream stream2 = File.Open(path, FileMode.OpenOrCreate))
        using (StreamWriter sWriter1 = new StreamWriter(stream2))
        {
            if (recievdata.Contains("UCAST") && recievdata.Contains("=g"))
            {
                sWriter1.Write(DateTime.Now.ToString("HH:mm:ss"));
                sWriter1.Write(" ; ");
                sWriter1.WriteLine(1);
                if (SleepMovChar.InvokeRequired)
                {
                    SleepMovChar.Invoke(new MethodInvoker(delegate
                    { SleepMovChar.Series["Bevægelse"].Points.AddXY(xValue = DateTime.Now.ToString("HH:mm:ss"), 1); }));
                    SleepMovChar.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
                }
            }
            else
            {
                sWriter1.Write(DateTime.Now.ToString("HH:mm:ss"));
                sWriter1.Write(" ; ");
                sWriter1.WriteLine(0);
                timer1.Tick += new EventHandler(timer1_Tick);
            }
        }
    }

0 个答案:

没有答案