-2

I have the following code, it is not doing what I want it to do. Can anyone help? This is a timer event project. The application is collecting value and writing it to the .csv file.

This is what I am trying to do:

  • If .txt file is missing, rename the .csv as .txt
  • rename .csv as .txt,(Whenever .txt file is missing rename .csv as txt)--> this is not working
  • If .csv is missing create new one and continue to append to it--> this is working

            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
    
Smoky2016
  • 21
  • 1
  • 10

2 Answers2

0

Try the following code:

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
Hadi
  • 36,233
  • 13
  • 65
  • 124
0

If you are having this problem in the future, here is what I did to solve this problem: I hope this help

            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
Smoky2016
  • 21
  • 1
  • 10