它正在写入txt文件文件,但每行只有1个字段。我需要在文本文件中的每个字段之间使用逗号,以便每条记录在1行。我该怎么做?
这是我的代码的这一部分。
Imports System.IO
Public Class ContactInfoForm
' Declare module-level variable.
Private ContactInfoStreamWriter As StreamWriter
Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
' Save the users contact information to the end of the file.
' Make sure name field and at least 1 number field is not empty.
If NameTextBox.Text <> "" And PhoneNumberTextBox.Text <> "" Or PagerNumberTextBox.Text <> "" Or
CellPhoneNumberTextBox.Text <> "" Or VoiceMailNumberTextBox.Text <> "" Then
If ContactInfoStreamWriter IsNot Nothing Then ' Check if the file is open
ContactInfoStreamWriter.WriteLine(NameTextBox.Text)
ContactInfoStreamWriter.WriteLine(PhoneNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(PagerNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(CellPhoneNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(VoiceMailNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(EmailAddressTextBox.Text)
With NameTextBox
.Clear()
.Focus()
End With
PhoneNumberTextBox.Clear()
PagerNumberTextBox.Clear()
CellPhoneNumberTextBox.Clear()
VoiceMailNumberTextBox.Clear()
EmailAddressTextBox.Clear()
Else ' File is not open
MessageBox.Show("You must open the file before you can save your contact information.", "File is Not Open",
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Display the File Open dialog box.
OpenToolStripMenuItem_Click(sender, e)
End If
Else
MessageBox.Show("Please enter your name and at least 1 number where you can be reached.", "Data Entry Error",
MessageBoxButtons.OK)
NameTextBox.Focus()
End If
End Sub
答案 0 :(得分:1)
这是因为您使用WriteLine将其参数写入一行。您可以使用.Write()或使用String.Format(),例如:
Dim info As String = ""
info = String.Format("{0},{1},{2},{3},{4},{5}", _
NameTextBox.Text, PhoneNumberTextBox.Text, PagerNumberTextBox.Text, CellPhoneNumberTextBox.Text, VoiceMailNumberTextBox.Text, EmailAddressTextBox.Text)
ContactInfoStreamWriter.WriteLine(info)