我有循环,它为我创建的每个tabPage创建了txt文件。现在每个循环它只是替换旧的“sml.txt”但我想创建sml1.txt,sml2.txt,sml3.txt ... ...
Dim FileWriter As StreamWriter
System.IO.Directory.CreateDirectory("E:\CEE")
System.IO.Directory.CreateDirectory("E:\CEE\smlouvy")
For i = 1 To tabpagelist.LongCount
FileWriter = New StreamWriter("E:\CEE\smlouvy\sml.txt", False) ----
--Here i want every loop create new txt (sml & counter)
For x = 0 To tabControls(tabpagelist(i)).LongCount - 1
FileWriter.WriteLine(tabControls(tabpagelist(i))(x).Name & "|" & tabControls(tabpagelist(i))(x).Text)
Next
FileWriter.Flush()
FileWriter.Close()
Next
答案 0 :(得分:1)
您可以在文件名末尾添加加号:
Dim FileWriter As StreamWriter
System.IO.Directory.CreateDirectory("E:\CEE")
System.IO.Directory.CreateDirectory("E:\CEE\smlouvy")
For i = 1 To tabpagelist.LongCount
FileWriter = New StreamWriter("E:\CEE\smlouvy\sml.txt" + i.toString, False)
For x = 0 To tabControls(tabpagelist(i)).LongCount - 1
FileWriter.WriteLine(tabControls(tabpagelist(i))(x).Name & "|" &
tabControls(tabpagelist(i))(x).Text)
Next
FileWriter.Flush()
FileWriter.Close()
Next
答案 1 :(得分:0)
尝试
For i = 1 To tabpagelist.LongCount
'Here i want every loop create new txt (sml & counter)
Dim filename As String = String.Format("E:\CEE\smlouvy\sml{0}.txt", i)
FileWriter = New StreamWriter(filename, False) ---
For x = 0 To tabControls(tabpagelist(i)).LongCount - 1
FileWriter.WriteLine(tabControls(tabpagelist(i))(x).Name & "|" & tabControls(tabpagelist(i))(x).Text)
Next
FileWriter.Flush()
FileWriter.Close()
Next
答案 2 :(得分:0)
使用Path.Combine
创建目录。
System.IO.Directory.CreateDirectory
创建所有必需的子目录,因此您不必再调用它两次。
使用Using ... End Using
确保正确关闭和处理流。
Dim basePath As String = Path.Combine("E:\CEE", "smlouvy")
System.IO.Directory.CreateDirectory(basePath)
For i = 1 To tabpagelist.LongCount
Dim txtPath = Path.Combine(basePath, String.Concat("sml", i, ".txt"))
Using FileWriter = New StreamWriter(txtPath, False)
For x = 0 To tabControls(tabpagelist(i)).LongCount - 1
FileWriter.WriteLine(tabControls(tabpagelist(i))(x).Name & "|" & tabControls(tabpagelist(i))(x).Text)
Next
End Using
Next