在 Visual Basic 中重命名文件

Rename file in visual basic

我有以下代码,它没有按照我的意愿执行。谁能帮忙? 这是一个定时器事件项目。应用程序正在收集值并将其写入 .csv 文件。

这就是我想要做的:

试试下面的代码:

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

如果您以后遇到这个问题,以下是我为解决这个问题所做的工作: 希望对您有所帮助

            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