积极答复者
想请教各位我可以如何加入标题在我save 的txt file。

问题
-
目前我的text file 的 内容是下图。
我想add date/ time/ UserName 的title
Public Sub ProceedSave1()
Dim strdatenow As String
Dim strtime As String
Dim UserName As String
UserName = pw.Text
strCurrPath = (Application.StartupPath)
strCurrPath = Replace(Application.StartupPath, "\bin\Debug", "")
Try
Dim file As System.IO.StreamWriter
strdatenow = Format(Now, "dd-MM-yyyy")
strtime = Format(Now, "HH-mm")
file = My.Computer.FileSystem.OpenTextFileWriter(strCurrPath + "\User\" + strdatenow + ".txt", True)
file.WriteLine(strdatenow + ">>" + strtime + ">>" + UserName)
file.Close()
Catch ex As Exception
End Try
End Sub
答案
-
Hi christing,
我根据你的代码做了一个测试,你可以将’添加标题和第一次数据’与’继续添加数据’两个部分区分开。
参考如下代码。
Dim sw As System.IO.StreamWriter Dim UserName As String Dim strtime As String = ">>>" Dim strdatenow As String Dim path As String = "D:\\TestFile2\\test.txt" Dim Count As String = 0 Public Sub ProceedSaveUser() UserName = TextBox1.Text strdatenow = DateTimePicker1.Text Try sw = My.Computer.FileSystem.OpenTextFileWriter(path, True) sw.WriteLine("Count=1") Dim lenCount As Integer = 0 Dim titleString As String = "Title:" lenCount = strdatenow.ToString().Length titleString = titleString.PadRight((lenCount + 2) / 2) + "Time" lenCount += strtime.Length + 2 titleString = titleString.PadRight(lenCount) + "User" sw.WriteLine(titleString) sw.WriteLine(strdatenow + ">>" + strtime + UserName) sw.Close() Catch ex As Exception End Try End Sub Public Sub ContinueSave(ByVal path As String) UserName = TextBox1.Text strdatenow = DateTimePicker1.Text sw = My.Computer.FileSystem.OpenTextFileWriter(path, True) sw.WriteLine(strdatenow + ">>" + strtime + UserName) sw.Close() updateTxtCount(path) End Sub Private Sub updateTxtCount(ByVal path As String) Dim array As String() = File.ReadAllLines(path) For Each arr In array If arr.Contains(">>") Then Count += 1 End If Next For i As Integer = 0 To array.Length - 1 If array(i).Contains("Count=") Then array(i) = "Count=" & Count End If Next File.WriteAllLines(path, array) Count = 0 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ProceedSaveUser() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ContinueSave(path) End Sub
测试结果:
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 christing 2019年12月31日 8:56
全部回复
-
Hi christing,
关于你的问题我有一点需要确认一下。
>>我想add date/ time/ UserName 的title
你是想要将标题的内容添加到txt文件中还是想重命名这个txt文件呢?
期待你的更新。
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi christing,
根据你的问题,我做了一个测试,下面是你可以参考的代码。
Dim UserName As String = "Admin" Dim path As String Dim strdatenow As String = "08-16" Dim strtime As String = "20-12-2019" Dim count As Integer = 2 ' Your number count path = "file path" Try Dim sw As System.IO.StreamWriter sw = My.Computer.FileSystem.OpenTextFileWriter(path, True) sw.WriteLine($"COUNT={count}") Dim lenCount As Integer = 0 Dim titleString As String = "Date" lenCount = strdatenow.Length + 2 titleString = titleString.PadRight(lenCount) + "Time" lenCount += strtime.Length + 2 titleString = titleString.PadRight(lenCount) + "User" sw.WriteLine(titleString) sw.WriteLine(strdatenow + ">>" + strtime + ">>" + UserName) sw.Close() Catch ex As Exception End Try
测试结果.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已建议为答案 Xingyu ZhaoMicrosoft contingent staff, Moderator 2019年12月24日 8:05
-
Hi christing,
‘count’记录了你总共要执行多少次 ’sw.WriteLine(strdatenow + ">>" + strtime + ">>" + UserName)’ 的操作,你可以在这样的操作执行之前,计算出要写入多少数据,比如如果你的数据来自’datatable’,你可以根据’datatable’的行数计算出’count’。
另外,你也可以使用下面的方法实时更新 ‘count’。
Dim count As Integer = 0 Private Sub updateTxtCount(ByVal path As String) Dim array As String() = File.ReadAllLines(path) For Each arr In array If arr.Contains(">>") Then count += 1 End If Next For i As Integer = 0 To array.Length - 1 If array(i).Contains("COUNT=") Then array(i) = "COUNT=" & count End If Next File.WriteAllLines(path, array) End Sub
结果:
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
感谢你的回复以及指教
我想再次请教我的program可以如何篇写后,得到每当有新data进入时不会在增加 title 以及count如下图表
我的program呈现的result是每当我有新打他进来就会insert如下图的数据
以下是我的program请查看
Public Sub ProceedSaveUser()
Dim UserName As String = Me.pw.Text
Dim strdatenow As String = Form2.DateTimePicker1.Text
'Dim strdatenow As String = "TEST"
Dim strtime As String = ">>>"
Dim Count As String = 0
Count = Count + 1
strCurrPath = (Application.StartupPath)
strCurrPath = Replace(Application.StartupPath, "\bin\Debug", "")
Try
Dim sw As System.IO.StreamWriter
sw = My.Computer.FileSystem.OpenTextFileWriter(strCurrPath + "\DUser\" + strdatenow + ".txt", True)
sw.WriteLine("Count=" + Count)
Dim lenCount As Integer = 0
Dim titleString As String = "Title"
lenCount = strdatenow.Length + 0
titleString = titleString.PadRight(lenCount) + "Time"
lenCount += strtime.Length + 21
titleString = titleString.PadRight(lenCount) + "User"
sw.WriteLine(titleString)
sw.WriteLine(strdatenow + ">>" + strtime + UserName)
'sw.WriteLine(vbNewLine)
sw.Close()
Catch ex As Exception
End Try
End Sub
-
Hi christing,
我根据你的代码做了一个测试,你可以将’添加标题和第一次数据’与’继续添加数据’两个部分区分开。
参考如下代码。
Dim sw As System.IO.StreamWriter Dim UserName As String Dim strtime As String = ">>>" Dim strdatenow As String Dim path As String = "D:\\TestFile2\\test.txt" Dim Count As String = 0 Public Sub ProceedSaveUser() UserName = TextBox1.Text strdatenow = DateTimePicker1.Text Try sw = My.Computer.FileSystem.OpenTextFileWriter(path, True) sw.WriteLine("Count=1") Dim lenCount As Integer = 0 Dim titleString As String = "Title:" lenCount = strdatenow.ToString().Length titleString = titleString.PadRight((lenCount + 2) / 2) + "Time" lenCount += strtime.Length + 2 titleString = titleString.PadRight(lenCount) + "User" sw.WriteLine(titleString) sw.WriteLine(strdatenow + ">>" + strtime + UserName) sw.Close() Catch ex As Exception End Try End Sub Public Sub ContinueSave(ByVal path As String) UserName = TextBox1.Text strdatenow = DateTimePicker1.Text sw = My.Computer.FileSystem.OpenTextFileWriter(path, True) sw.WriteLine(strdatenow + ">>" + strtime + UserName) sw.Close() updateTxtCount(path) End Sub Private Sub updateTxtCount(ByVal path As String) Dim array As String() = File.ReadAllLines(path) For Each arr In array If arr.Contains(">>") Then Count += 1 End If Next For i As Integer = 0 To array.Length - 1 If array(i).Contains("Count=") Then array(i) = "Count=" & Count End If Next File.WriteAllLines(path, array) Count = 0 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ProceedSaveUser() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ContinueSave(path) End Sub
测试结果:
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 christing 2019年12月31日 8:56