cmdolcet
asked on
how to close and re-open a stream reader object
I am running into some issues where I throw an error when my file location is not available and in my catch statement I want to select a new file location that does exist.
Here is the code:
Try
'Starts the process to Write the file in the defined export location.
Dim Stream As FileStream = New FileStream(exportFile.Full Name, FileMode.Append)
Dim streamWriter As New StreamWriter(Stream)
streamWriter.Write(strExpo rtHeader & strExportText)
streamWriter.Close()
Stream.Close()
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch dirEx As DirectoryNotFoundException
mainFormStatusStripLabel.F ont = New Font(mainFormStatusStripLa bel.Font.F ontFamily, 8, FontStyle.Regular)
mainFormStatusStripLabel.B ackColor = System.Drawing.Color.Red
mainFormStatusStripLabel.T ext = "The following export directory was not found..."
Dim Stream As FileStream = New FileStream(masterSettings. masterExpo rt.masterE xportOffli neLocation , FileMode.Append)
Dim streamWriter As New StreamWriter(Stream)
streamWriter.Write(strExpo rtHeader & strExportText)
streamWriter.Close()
Stream.Close()
Catch wio As IOException
Dim errStr As String
errStr = "The following export could not be written to. The file could be in use or corrupt..." & vbCrLf & vbCrLf
errStr += exportFile.FullName & vbCrLf & vbCrLf
errStr += "Press OK to select as temporary directory. NOTE: This must be changed in the Configuration or this dialog will continue to appear."
MessageBox.Show(errStr, "File In Use", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return -10
End Try
Here is the code:
Try
'Starts the process to Write the file in the defined export location.
Dim Stream As FileStream = New FileStream(exportFile.Full
Dim streamWriter As New StreamWriter(Stream)
streamWriter.Write(strExpo
streamWriter.Close()
Stream.Close()
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch dirEx As DirectoryNotFoundException
mainFormStatusStripLabel.F
mainFormStatusStripLabel.B
mainFormStatusStripLabel.T
Dim Stream As FileStream = New FileStream(masterSettings.
Dim streamWriter As New StreamWriter(Stream)
streamWriter.Write(strExpo
streamWriter.Close()
Stream.Close()
Catch wio As IOException
Dim errStr As String
errStr = "The following export could not be written to. The file could be in use or corrupt..." & vbCrLf & vbCrLf
errStr += exportFile.FullName & vbCrLf & vbCrLf
errStr += "Press OK to select as temporary directory. NOTE: This must be changed in the Configuration or this dialog will continue to appear."
MessageBox.Show(errStr, "File In Use", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return -10
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It says fileStream is a type and cannot be used as an expression?
Both methods work for me:
Test code:
-saige-
Test code:
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Running example 1: {0}", Example1())
Console.WriteLine("Running example 2: {0}", Example2())
Console.ReadLine()
End Sub
Function Example1() As Integer
Dim [stream] As FileStream = Nothing
Dim [writer] As StreamWriter = Nothing
Try
'Starts the process to Write the file in the defined export location.
[stream] = New FileStream("C:\_admin\EE_Q28974184\testing1\testingex1.txt", FileMode.Append)
[writer] = New StreamWriter([stream])
[writer].Write("This is a test of example1")
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch dirEx As DirectoryNotFoundException
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
Try
[stream] = New FileStream("C:\_admin\EE_Q28974184\testingex1.txt", FileMode.Append)
[writer] = New StreamWriter([stream])
[writer].Write("This is a test of example1")
Finally
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
End Try
Catch wio As IOException
Console.WriteLine(wio.Message)
Return -10
Finally
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
End Try
End Function
Function Example2() As Integer
Try
'Starts the process to Write the file in the defined export location.
Using [stream] As New FileStream("C:\_admin\EE_Q28974184\testing2\testingex2.txt", FileMode.Append)
Using [writer] As New StreamWriter([stream])
[writer].Write("This is a test of example2")
End Using
End Using
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch dirEx As DirectoryNotFoundException
Using [stream] As New FileStream("C:\_admin\EE_Q28974184\testingex2.txt", FileMode.Append)
Using [writer] As New StreamWriter([stream])
[writer].Write("This is a test of example2")
End Using
End Using
Catch wio As IOException
Console.WriteLine(wio.Message)
Return -10
End Try
End Function
End Module
Test folders:Test output:Successful output:Exceptional output:-saige-
It is possible, though, that you have a class or module that is named fileStream. If this is the case try explicitly declaring the usage of filestream; e.g. -
Module Module1
Sub Main()
Console.WriteLine("Running example 1: {0}", Example1())
Console.WriteLine("Running example 2: {0}", Example2())
Console.ReadLine()
End Sub
Function Example1() As Integer
Dim [stream] As System.IO.FileStream = Nothing
Dim [writer] As System.IO.StreamWriter = Nothing
Try
'Starts the process to Write the file in the defined export location.
[stream] = New System.IO.FileStream("C:\_admin\EE_Q28974184\testing1\testingex1.txt", System.IO.FileMode.Append)
[writer] = New System.IO.StreamWriter([stream])
[writer].Write("This is a test of example1")
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch ex As System.IO.DirectoryNotFoundException
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
Try
[stream] = New System.IO.FileStream("C:\_admin\EE_Q28974184\testingex1.txt", System.IO.FileMode.Append)
[writer] = New System.IO.StreamWriter([stream])
[writer].Write("This is a test of example1")
Finally
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
End Try
Catch ex As System.IO.IOException
Console.WriteLine(ex.Message)
Return -10
Finally
If [writer] IsNot Nothing Then [writer].Dispose()
If [stream] IsNot Nothing Then [stream].Dispose()
[writer] = Nothing
[stream] = Nothing
End Try
End Function
Function Example2() As Integer
Try
'Starts the process to Write the file in the defined export location.
Using [stream] As New System.IO.FileStream("C:\_admin\EE_Q28974184\testing2\testingex2.txt", System.IO.FileMode.Append)
Using [writer] As New System.IO.StreamWriter([stream])
[writer].Write("This is a test of example2")
End Using
End Using
'This is triggered if export folder has change or has been deleted and canoot find the Partfile Export location.
Catch ex As System.IO.DirectoryNotFoundException
Using [stream] As New System.IO.FileStream("C:\_admin\EE_Q28974184\testingex2.txt", System.IO.FileMode.Append)
Using [writer] As New System.IO.StreamWriter([stream])
[writer].Write("This is a test of example2")
End Using
End Using
Catch ex As System.IO.IOException
Console.WriteLine(ex.Message)
Return -10
End Try
End Function
End Module
ASKER
Taking your first example into my code. I get to the:
Try
[stream] = New FileStream(masterSettings.
and ten it jumps to the Finally statement.
Any idea as to why?