1
我想避免重複代碼以有條件地寫入日誌文件,正如我在下面所做的。在VB.net中有更好的解決方案嗎?在VB.net中使用的條件定義
If Not System.IO.File.Exists(log_file_address) Then
Using log_file_stream as New System.IO.StreamWriter(log_file_address)
log_file_stream.Writeline (current_time_string)
log_file_stream.Writeline (" ")
log_file_stream.Writeline (log_message)
log_file_stream.Close()
End Using
Else
Using log_file_stream As IO.StreamWriter = System.IO.File.AppendText(log_file_address)
log_file_stream.Writeline (current_time_string)
log_file_stream.Writeline (" ")
log_file_stream.Writeline (log_message)
log_file_stream.Close()
End Using
End If
也許是這樣的:
Using log_file_stream as If(System.IO.File.Exists(log_file_address), _
System.IO.File.AppendText(log_file_address), _
New System.IO.StreamWriter(log_file_address))
log_file_stream.Writeline (current_time_string)
log_file_stream.Writeline (" ")
log_file_stream.Writeline (log_message)
log_file_stream.Close()
End Using
只是'System.IO.File.Exists(log_file_address)' –
謝謝,非常乾淨,善於洞察到哪裏尋找這些各種各樣的問題,更換'TRUE'。 – Dpodz