在 Visual Basic 中重命名文件
Rename file in visual basic
我有以下代码,它没有按照我的意愿执行。谁能帮忙?
这是一个定时器事件项目。应用程序正在收集值并将其写入 .csv 文件。
这就是我想要做的:
- 如果 .txt 文件丢失,请将 .csv 重命名为 .txt
- 将 .csv 重命名为 .txt,(每当缺少 .txt 文件时将 .csv 重命名为 txt)--> 这不起作用
如果 .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
试试下面的代码:
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
我有以下代码,它没有按照我的意愿执行。谁能帮忙? 这是一个定时器事件项目。应用程序正在收集值并将其写入 .csv 文件。
这就是我想要做的:
- 如果 .txt 文件丢失,请将 .csv 重命名为 .txt
- 将 .csv 重命名为 .txt,(每当缺少 .txt 文件时将 .csv 重命名为 txt)--> 这不起作用
如果 .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
试试下面的代码:
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