我有以下代码,它没有做我想做的事情。有人可以帮忙吗? 这是一个计时器事件项目。应用程序正在收集值并将其写入.csv文件。
这就是我想要做的事情:
如果缺少.csv,请创建新的并继续附加到其中 - >这是有效的
Try
'Check for .csv file
If My.Computer.FileSystem.FileExists(csv_File) = False Then
Else 'if file does not exist, create new file to start writing to it
My.Computer.FileSystem.WriteAllText(csv_File, vbCrLf & Today & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
End If
If My.Computer.FileSystem.FileExists(txt_File) = False Then
FileOpen(1, csv_File, OpenMode.Output)
FileClose(1)
'Check for .txt
If File.Exists(txt_File) = False Then
' Change ".CSV" to the path and filename for the file that
My.Computer.FileSystem.RenameFile(csv_File, txt_File)
End If
End If
Catch ex As Exception
End Try
答案 0 :(得分:0)
请尝试以下代码:
If not io.file.exists(csv_file) then
Using strw as new io.streamwriter(csv_file)
Strw.write(vbCrLf & Today & "," & Tag_name_Read & "," & itemValues(0).Value.ToString)
Strw.close()
End using
End if
If not io.file.exists(txt_file) then
Io.file.move(csv_file,txt_file)
End if
答案 1 :(得分:0)
如果您将来遇到此问题,我将采取以下措施解决此问题: 我希望这有帮助
Try
'Check for .csv file
If My.Computer.FileSystem.FileExists(csv_File) = False Then
Else
'if file does not exist, create new file to start writing to it
My.Computer.FileSystem.WriteAllText(csv_File, vbCrLf & Today & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
If My.Computer.FileSystem.FileExists(txt_File) = False Then
FileClose()
' Change ".CSV" to the path and filename for the file that you want to rename
My.Computer.FileSystem.RenameFile(csv_File, txt_File)
'File Header
My.Computer.FileSystem.WriteAllText(csv_File, "Date and Time, Tag Name, Value", False)
'Append file
My.Computer.FileSystem.WriteAllText(csv_File, vbCrLf & Today & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
End If
End If
Catch ex As Exception
End Try