2016-11-11 44 views
-2

我有下面的代码,它没有做我想做的事。谁能帮忙? 这是一个计时器事件项目。该应用程序正在收集价值并将其写入.csv文件。在Visual Basic中重命名文件

这就是我要做的:

  • 如果.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 
    
+0

它正在做什么? –

+0

这不是重命名.csv文件 – Smoky2016

+0

你的问题没有多大意义....你想达到什么目的? – Werdna

回答

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 
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