我创建了VALIDCCC.TXT,我需要在此文本文件中写入一些内容,内容如下所示
内容:
00 A0
00 A1
00 A2
10 A0
10 A1
我的守则如下:
filepath = filepath + "\" + strsubmenu + "\"
If (Not System.IO.Directory.Exists(filepath)) Then
System.IO.Directory.CreateDirectory(filepath)
End If
filepath = filepath + "MATRIX\"
If (Not System.IO.Directory.Exists(filepath)) Then
System.IO.Directory.CreateDirectory(filepath)
End If
filepath = filepath + "VALIDCCC.TXT"
If (Not System.IO.Directory.Exists(filepath)) Then
File.Create(filepath)
End If
File.WriteAllText(filepath, String.Empty)
Dim objWriter As New System.IO.StreamWriter(filepath, True)
For Each Customeritem As ListItem In CustomerCodeDVListBox.Items
For Each CCCitem As ListItem In CCCListBox.Items
objWriter.WriteLine(Customeritem.ToString() + Space(4) + CCCitem.ToString())
Next
Next
我有2个列表框组合这个列表框的选择值应写入Textfile。 在写入内容之前,如果存在VALIDCCC.TXT,我需要清除内容或使用新内容覆盖...
答案 0 :(得分:0)
这是有效的......
filepath = filepath + "VALIDCCC.TXT"
Dim objWriter As StreamWriter
Dim sb As New StringBuilder
For Each Customeritem As ListItem In CustomerCodeDVListBox.Items
If Customeritem.Selected Then
For Each CCCitem As ListItem In CCCListBox.Items
If CCCitem.Selected Then
sb.Append(Customeritem.ToString() + Space(4) + CCCitem.ToString())
sb.Append(Environment.NewLine)
End If
Next
End If
Next
If (Not System.IO.Directory.Exists(filepath)) Then
objWriter = File.CreateText(filepath)
objWriter.WriteLine(sb.ToString())
objWriter.Close()
End If
答案 1 :(得分:-1)
看看这是否有帮助:
filepath = filepath + "\" + strsubmenu + "\MATRIX\VALIDCCC.TXT"
Dim DirPath As String = Path.GetDirectoryName(filepath)
If Not Directory.Exists(dirpath) Then
Directory.CreateDirectory(dirpath)
End If
Dim objWriter As New System.IO.StreamWriter(filepath)
For Each Customeritem As ListItem In CustomerCodeDVListBox.Items
For Each CCCitem As ListItem In CCCListBox.Items
objWriter.WriteLine(Customeritem.ToString() + Space(4) + CCCitem.ToString())
Next
Next
这将根据需要创建任何文件夹,并根据需要覆盖或创建文本文件。