添加代码修改行 [英] code modified lines added

查看:97
本文介绍了添加代码修改行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据

date                    value
24sep2014 2:23:01        0.1
24sep2014 2:23:02        0.3
24sep2014 2:23:03        0.2
24sep2014 2:23:04        0.3

这些不是逗号分隔的值.我想用CSV文件编写.为下一行添加值.

These are not coma seprated value. I wanted to write in CSV file. Apend the value for next row.

1)如何在这里仅打开文件一次.下次运行时,文件名必须更改为其他名称 2)如何附加下一个值

1)How to open file only once here. when it run next time file name has to change to other name 2) How to append the next values

   Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
Imports System.Drawing
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    Dim myPort As Array
    Dim Distance As Integer



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        PortComboBox.Items.AddRange(myPort)
        BaudComboBox.Items.Add(9600)
        BaudComboBox.Items.Add(19200)
        BaudComboBox.Items.Add(38400)
        BaudComboBox.Items.Add(57600)
        BaudComboBox.Items.Add(115200)
        ConnectButton.Enabled = True
        DisconnectButton.Enabled = False

        Chart1.Series.Clear()
        Chart1.Titles.Add("Demo")
        'Create a new series and add data points to it.
        Dim s As New Series
        s.Name = "CURRENT"
        s.ChartType = SeriesChartType.Line

    End Sub

    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
        SerialPort1.PortName = PortComboBox.Text
        SerialPort1.BaudRate = BaudComboBox.Text
        SerialPort1.Open()
        Timer1.Start()
        Timer2.Start()
        'lblMessage.Text = PortComboBox.Text & " Connected."
        ConnectButton.Enabled = False
        DisconnectButton.Enabled = True
    End Sub

    Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
        SerialPort1.Close()

        DisconnectButton.Enabled = False
        ConnectButton.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim counter As Integer
        counter = 0


        Try

            SerialPort1.Write("c")
            System.Threading.Thread.Sleep(250)
            Dim k As Double
            Dim distance As String = SerialPort1.ReadLine()
            k = CDbl(distance)
            ListBoxSensor.Text = k

            Dim s As New Series
            s.Points.AddXY(1000, k)
            Chart1.Series.Add(s)

            Dim headerText = ""
            Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")

            If Not File.Exists(csvFile)) Then
                headerText = "Date& time ,Current"
            End If

            Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
                If headerText.Length > 0 Then
                    outFile.WriteLine(headerText)
                End If
                Dim y As String = DateAndTime.Now
                Dim x As String = y + "," + distance
                outFile.WriteLine(x)
            End Using

        Catch ex As Exception

        End Try



    End Sub

    Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
        SerialPort1.Write("1")
    End Sub

    Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
        SerialPort1.Write("0")
    End Sub




End Class

在这里,我一次又一次打开文件.因此我只能存储一个值

Here i am opening file again and again. that reason i can store only one value

@史蒂夫(Steve)错误

@ steve error

推荐答案

The second parameter of OpenTextFileWriter allows to append instead of overwrite your file.
So it is simply a matter to check if the file exists (so you insert the header names) and then write your data

Dim headerText = ""
Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not File.Exists(csvFile) Then
    headerText = "Date& time ,Current"
End If 

Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
    If headerText.Length > 0 Then 
        outFile.WriteLine(headerText)
    End If
    Dim y As String = DateAndTime.Now
    Dim x As String = y + "," + distance
    outFile.WriteLine(x)
End Using

请注意使用声明,以确保关闭并处置文件资源也有例外.

Notice the Using Statement to be sure to close and dispose the file resource also in case of exceptions.

但是,鉴于需要编写的简单文本,您还可以选择使用方法

However given the simple text that need to be written you could also choose to use the method WriteAllText

Dim headerText = ""
Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not File.Exists(csvFile) Then
    headerText = "Date& time ,Current" & Environment.NewLine
End If 

Dim y As String = DateAndTime.Now
Dim x As String = headerText & y & "," + distance & Environment.NewLine
My.Computer.FileSystem.WriteAllText(csvFile, x, True)

这篇关于添加代码修改行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆