.net 如何保存文件而不引发异常错误
我有以下代码,在保存文件时可以很好地工作。但是,当“保存到”对话框以“保存”或“取消”选项打开时……当我单击“取消”时,我在此处获取异常错误。 W.WriteLine(gradesListBox.Items(1).ToString)我该如何解决这个问题?我已经试过了,但没有成功.net 如何保存文件而不引发异常错误,.net,vb.net,.net,Vb.net,我有以下代码,在保存文件时可以很好地工作。但是,当“保存到”对话框以“保存”或“取消”选项打开时……当我单击“取消”时,我在此处获取异常错误。 W.WriteLine(gradesListBox.Items(1).ToString)我该如何解决这个问题?我已经试过了,但没有成功 If result <> Windows.Forms.DialogResult.Cancel Then Close() End If Private Sub Button1_C
If result <> Windows.Forms.DialogResult.Cancel Then
Close()
End If
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result As DialogResult
'Writes txt File to C: Drive
SaveFileDialog1.InitialDirectory = ("C:\")
SaveFileDialog1.FileName = ("Save As...")
SaveFileDialog1.Filter = ("Only Text Files (*.txt)|*.txt")
SaveFileDialog1.ShowDialog()
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName, True) ' writes specified information to save file
W.WriteLine(lblTimeDate.Text & " " & txtJobFileName.Text)
W.WriteLine() 'Writes Blank Line
W.WriteLine()
'Formats Write to save file
W.WriteLine(vbTab & gradesListBox.Items(0).ToString)
W.WriteLine(gradesListBox.Items(1).ToString)
W.WriteLine(gradesListBox.Items(2).ToString)
W.WriteLine(gradesListBox.Items(3).ToString)
W.WriteLine(gradesListBox.Items(4).ToString)
W.WriteLine(gradesListBox.Items(6).ToString)
W.WriteLine(gradesListBox.Items(9).ToString)
W.WriteLine(gradesListBox.Items(7).ToString)
W.WriteLine(gradesListBox.Items(8).ToString)
W.WriteLine(gradesListBox.Items(9).ToString)
W.WriteLine(gradesListBox.Items(10).ToString)
W.WriteLine()
W.WriteLine()
如果结果为Windows.Forms.DialogResult.Cancel,则
关闭()
如果结束
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
将结果变暗为对话框结果
'将txt文件写入C:驱动器
SaveFileDialog1.InitialDirectory=(“C:\”)
SaveFileDialog1.FileName=(“另存为…”)
SaveFileDialog1.Filter=(“仅文本文件(*.txt)|*.txt”)
SaveFileDialog1.ShowDialog()
Dim W作为新IO.StreamWriter(SaveFileDialog1.FileName,True)将指定信息写入保存文件
W.WriteLine(lblTimeDate.Text&&txtJobFileName.Text)
W.WriteLine()'写入空行
W.WriteLine()
'格式化写入以保存文件
W.WriteLine(vbTab和gradesListBox.Items(0.ToString)
W.WriteLine(GradesLstBox.Items(1.ToString)
W.WriteLine(GradesLstBox.Items(2.ToString)
W.WriteLine(GradesLstBox.Items(3.ToString)
W.WriteLine(成绩列表框项目(4)至字符串)
W.WriteLine(成绩列表框。项目(6)。ToString)
W.WriteLine(GradesLstBox.Items(9.ToString)
W.WriteLine(GradesLstBox.Items(7.ToString)
W.WriteLine(GradesLstBox.Items(8.ToString)
W.WriteLine(GradesLstBox.Items(9.ToString)
W.WriteLine(GradesLstBox.Items(10.ToString)
W.WriteLine()
W.WriteLine()
这是一个非常简单的解决方案,谢谢。我还有一个问题,但我会留到下次再问
If SaveFileDialog1.ShowDialog <> DialogResult.Cancel Then
' do all that stuff
End If
For n as integer=0 to gradesListBox.items.count-1
W.WriteLine(gradesListBox.Items(n).ToString)
next n