Like I said, I'd probably declare it like the code I showed since you are
doing it recursive and all.
But...
Sub ReadAllFiles
Dim WriteTxtUpdateF orThisSub as StreamWriter = File.Create(... .)
Call ReadDirs(WHATEV ER_PATH, True, WriteTxtUpdateF orThisSub)
WriteTxtUpdateF orThisSub.Flush
WriteTxtUpdateF orThisSub.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean, ByRef
WriteTxtUpdate as StreamWriter)
Sourcedir &= Path.DirectoryS eparatorChar
If fRecursive Then
For Each sDir In Directory.GetDi rectories(Sourc edir)
sDirInfo = New DirectoryInfo(s Dir)
ReadDirs(sDirIn fo.FullName, fRecursive, WriteTxtUpdate)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFi les(Sourcedir)
.WriteLine(sFil e)
Next
End With
End Sub
"Bob Hollness" <bo*@blockbuste r.com> wrote in message
news:en******** ******@TK2MSFTN GP12.phx.gbl...
How can I declare it and then pass it to the Sub? This is new to me...
sorry..... :-(
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_ Please.com>
wrote in message news:ul******** ******@TK2MSFTN GP14.phx.gbl... You are talking about "2nd time" meaning the second time you call the
function ReadAllFiles right. If that is the case then my solution in my
previous post solves the problem. After you close WriteTextUpdate you
have to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs
2. (Probably a better way for this case, as I stated before) Define then
object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles
WriteTxtUpdate = File.CreateText File(........)
Call ReadDirs(WHATEV ER_PATH, True)
WriteTxtUpdate. Flush
WriteTxtUpdate. Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every
call.
Chris
"Bob Hollness" <bo*@blockbuste r.com> wrote in message
news:uk******** ******@TK2MSFTN GP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors
on line ".WriteLine(sFi le) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEV ER_PATH, True)
WriteTxtUpdate. Flush
WriteTxtUpdate. Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectoryS eparatorChar
If fRecursive Then
For Each sDir In Directory.GetDi rectories(Sourc edir)
sDirInfo = New DirectoryInfo(s Dir)
ReadDirs(sDirIn fo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFi les(Sourcedir)
.WriteLine(sFil e)
Next
End With
End Sub
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_ Please.com>
wrote in message news:e0******** *****@TK2MSFTNG P12.phx.gbl...
I think I'm a little confused at what you need to do. But your problem
lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText (Application.St artupPath & "\Update.tx t")
end if
'Do Something
End Sub
Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText (Application.St artupPath & "\Update.tx t")
end if
'Do Something
End Sub
Public Sub DoSomethingThat MustCloseFile
Writer.Close
Writer = Nothing
End Sub
Now this way of doing it opens the file if it was closed. And uses the
same file if it was already opened. I think this is what you are
talking about doing. If not, can you post some sample code that shows
the problem more?
Chris
"Bob Hollness" <bo*@blockbuste r.com> wrote in message
news:Ok******** ******@TK2MSFTN GP14.phx.gbl...
> Hi all.
>
> I have a Sub that calls another sub. Both subs use a common object,
> so I used Public to declare it at the top of the module, as below.
>
> Public Writer As StreamWriter =
> File.CreateText (Application.St artupPath & "\Update.tx t")
>
> The first time my Sub runs it works fine. But the second time it
> fails telling me "Cannot write to a closed TextWriter. I am guessing
> that this is because the Public section is only read once instance of
> the application. I have to close the TextWriter so that I can use the
> file in another process. Any idea how I can either ensure that the
> Public section is re-read, or, provide the TextWriter to the 2nd Sub?
>
> --
> Bob Hollness
>
> -------------------------------------
> I'll have a B please Bob
>