現在、処理結果のログをテキストファイルに吐き出すアプリケーションを開発しているのですが前段階として事前学習している内容になります。
テキストボックスに入力した内容をテキストファイルに上書き、追加書き込みを行うものなのですが、入力されたテキストデータごとに自動的に採番を行いたいです。
ベースとなるコードを下記に記載いたします。
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'テキストファイルを上書きで保存する Dim sw As New System.IO.StreamWriter("....\log_output.txt", False, System.Text.Encoding.Default) '書込み sw.Write(TextBox1.Text) sw.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'テキストファイルを追加書込みで保存する Dim sw1 As New System.IO.StreamWriter("....\log_output.txt", True, System.Text.Encoding.Default) '書込み(上記で作成したファイルに書き込んでいます) sw1.Write("," & TextBox1.Text) sw1.Close() '結果 texebox1の内容追加 End Sub End Class
開発環境はVS2019 VB.NET にて行っています。
調べた中ではテキストファイル自体に採番することは出来るようなのですが
内容にあたる部分の採番の方法が考え付きません。
ご不明な点などがありましたらお申し付けください。
御助力の程宜しくお願い致します。
追記
改行するよう修正しました。
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'テキストファイルを上書きで保存する Dim sw As New System.IO.StreamWriter("....\log_output.txt", False, System.Text.Encoding.Default) '書込み sw.Write(TextBox1.Text) sw.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'テキストファイルを追加書込みで保存する Dim sw1 As New System.IO.StreamWriter("....\log_output.txt", True, System.Text.Encoding.Default) '書込み(上記で作成したファイルに書き込んでいます) sw1.Write("," & TextBox1.Text, Environment.NewLine) sw1.Close() '結果 texebox1の内容追加 End Sub End Class
回答2件