2016-11-11 46 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