[英]how to Write Content into textfile in vb.net
I Created VALIDCCC.TXT and I need to write some content into this text file, content as shown below 我创建了VALIDCCC.TXT,我需要将一些内容写入此文本文件,内容如下所示
Content : 内容:
00 A0
00 A1
00 A2
10 A0
10 A1
My Code as Follows : 我的代码如下:
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
I have 2 listboxes combination of this listbox seleted values should write into Textfile. 我有2个列表框,此列表框的组合值应该被写入Textfile中。 Before writing content if VALIDCCC.TXT exists I need to clear the content or overwrite with New Content...
如果存在VALIDCCC.TXT,则在编写内容之前,我需要清除内容或用新内容覆盖...
This is working ... 这正在工作...
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
See if this helps: 看看是否有帮助:
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
This will create any folders as necessary and overwrite or create the text file as needed. 这将根据需要创建任何文件夹,并根据需要覆盖或创建文本文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.