How many files can be open at a given time?

  • Thread starter Thread starter J e r
  • Start date Start date
J

J e r

I'm a novice using VB .NET . I'm trying to use 2 files to read a line write
the line and get the run time error
"..exception of type 'System.IO.IOException.....additional information:
File already open."

It seems the error message is telling me VB can have only 1 file open at a
time. Tell me that's not true and where
I'm going wrong, please.

The code is:

Imports System.IO.File
..
..
..
..
private sub bRun_Click(byval sender as System.Object, byval e as
System.Event.Args) handles bRun.Click)

dim infil as integer = FreeFile()
dim outfil as integer = FreeFile()
dim infilname as string = "c"\red.txt"
dim outfilname as string = "c"\wrt.txt"
dim inline as string

FileOpen(outfil, outfilname,OpenMode.Output)
FileOpen(infil,infilname,OpenMode.Input) 'this is where error
occurs

while not eof(infil)
inline = LineInput(infil)
PrintLine(outfil,inline)
end while

FileClose(infil)
FileClose(outfil)

end sub


Thanks for your time, J e r
 
Hi Jer,

I can give you an answer on your code (you can a lot of files open in the
same time), but this is very classic VB, when you are not writting something
that has to be converted, have than a look at streamreader and streamwriter
class.

I hope this helps

Cor
 
Right here is the most probable scenario !

You write some code which opens a file right !, then it fails or you are
debugging it and terminate it before you close it. OR you forget to close it
in code.

Then you change something and run the code again, you get the error message
because you already left it open.

Regards - OHM








I'm a novice using VB .NET . I'm trying to use 2 files to read a
line write the line and get the run time error
"..exception of type 'System.IO.IOException.....additional
information: File already open."

It seems the error message is telling me VB can have only 1 file open
at a time. Tell me that's not true and where
I'm going wrong, please.

The code is:

Imports System.IO.File
.
.
.
.
private sub bRun_Click(byval sender as System.Object, byval e as
System.Event.Args) handles bRun.Click)

dim infil as integer = FreeFile()
dim outfil as integer = FreeFile()
dim infilname as string = "c"\red.txt"
dim outfilname as string = "c"\wrt.txt"
dim inline as string

FileOpen(outfil, outfilname,OpenMode.Output)
FileOpen(infil,infilname,OpenMode.Input) 'this is where
error occurs

while not eof(infil)
inline = LineInput(infil)
PrintLine(outfil,inline)
end while

FileClose(infil)
FileClose(outfil)

end sub


Thanks for your time, J e r

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
* "J e r said:
I'm a novice using VB .NET . I'm trying to use 2 files to read a line write
the line and get the run time error
"..exception of type 'System.IO.IOException.....additional information:
File already open."

It seems the error message is telling me VB can have only 1 file open at a
time. Tell me that's not true and where
I'm going wrong, please.

The code is:

Imports System.IO.File
.
.
.
.
private sub bRun_Click(byval sender as System.Object, byval e as
System.Event.Args) handles bRun.Click)

dim infil as integer = FreeFile()
dim outfil as integer = FreeFile()
dim infilname as string = "c"\red.txt"
dim outfilname as string = "c"\wrt.txt"
dim inline as string

FileOpen(outfil, outfilname,OpenMode.Output)
FileOpen(infil,infilname,OpenMode.Input) 'this is where error
occurs

while not eof(infil)
inline = LineInput(infil)
PrintLine(outfil,inline)
end while

FileClose(infil)
FileClose(outfil)

The code should work, but the filenames are invalid. Maybe the files
are opened by an other application?
 
Errata (after testing the code):

Notice that I have changed the order of the commands:

* (e-mail address removed) (Herfried K. Wagner [MVP]) scripsit:
 
Sorry I did not see the code before down the page. Here is the solution. The
original code could not have worked because the way FreeFile works is to
give you the next available File handle. As you had not used the first
available handle before asking for the next one, the same number would have
been given. '1'.

Here is the amended code. ( tested )

Dim infilname As String = "c:\MyIn.txt"
Dim outfilname As String = "c:\MyOut.txt"
Dim inline As String

infil = FreeFile()

FileOpen(infil, infilname, OpenMode.Input) 'this is where error occurs()

outfil = FreeFile()

FileOpen(outfil, outfilname, OpenMode.Output)

Regards - OHM



Right here is the most probable scenario !

You write some code which opens a file right !, then it fails or you
are debugging it and terminate it before you close it. OR you forget
to close it in code.

Then you change something and run the code again, you get the error
message because you already left it open.

Regards - OHM










Regards - OHM# OneHandedMan{at}BTInternet{dot}com

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
Thanks OHM!

And thanks to all who responded.

Your response time is the best.

Have a Merry Christmas, J e r
 
Back
Top