IOException - Bad file name but NOT a permissions problem

T

teddysnips

My clients get the following exception message when they try to write
to a file:

<exception>
Server Error in '/MyApp' Application.

Bad file name or number.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.IO.IOException: Bad file name or number.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace
below.

Stack Trace:


[IOException: Bad file name or number.]
Microsoft.VisualBasic.FileSystem.Write(Int32 FileNumber, Object[]
Output) +82
MyApp.PrinterSelectionForm.PrintMyReport() +1241
</exception>

The code that throws this exception (much simplifed) is:

Public Sub PrintMyReport()

Dim rpt As New
CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim ds As New DataSet()
Dim rptName As String
Dim intFileNumber As Integer = FreeFile()

Try

' A whole bunch of code to print a report using Crystal Reports.
' Crystal throws an exception


Catch ex As Exception
'problem
FileOpen(intFileNumber, "C:\My_Debug\MyAPP_ERRORS.txt",
OpenMode.Append)
Write(intFileNumber, "Number: " & ex.Source & " Message: " &
ex.Message & " Stack Trace: " & ex.StackTrace & vbNewLine)

Finally
FileClose(intFileNumber)

End Try
End Sub

This problem has only just started happening - the application has
been in use since 2004.

My first thought was that the permissions on C:\My_Debug
\MyAPP_ERRORS.txt weren't correct. However, the file had been written
to as recently as ten days ago (when a completely different problem
happened). I asked the SysAdmin to double-check the permissions on
the folder and file, and he says they're fine.

So, if it's NOT a permissions problem, what else can it be?

Help!

Thanks

Edward
 
A

Alexey Smirnov

My clients get the following exception message when they try to write
to a file:

<exception>
Server Error in '/MyApp' Application.

Bad file name or number.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.IO.IOException: Bad file name or number.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace
below.

Stack Trace:

[IOException: Bad file name or number.]
Microsoft.VisualBasic.FileSystem.Write(Int32 FileNumber, Object[]
Output) +82
MyApp.PrinterSelectionForm.PrintMyReport() +1241
</exception>

The code that throws this exception (much simplifed) is:

Public Sub PrintMyReport()

Dim rpt As New
CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim ds As New DataSet()
Dim rptName As String
Dim intFileNumber As Integer = FreeFile()

Try

' A whole bunch of code to print a report using Crystal Reports.
' Crystal throws an exception

Catch ex As Exception
'problem
FileOpen(intFileNumber, "C:\My_Debug\MyAPP_ERRORS.txt",
OpenMode.Append)
Write(intFileNumber, "Number: " & ex.Source & " Message: " &
ex.Message & " Stack Trace: " & ex.StackTrace & vbNewLine)

Finally
FileClose(intFileNumber)

End Try
End Sub

This problem has only just started happening - the application has
been in use since 2004.

My first thought was that the permissions on C:\My_Debug
\MyAPP_ERRORS.txt weren't correct. However, the file had been written
to as recently as ten days ago (when a completely different problem
happened). I asked the SysAdmin to double-check the permissions on
the folder and file, and he says they're fine.

So, if it's NOT a permissions problem, what else can it be?

Hi Edward

FreeFile/FileOpen comes from VB6, I'm not sure why it stopped to work,
maybe you can call FreeFile right before the FileOpen

Like this

Catch ex As Exception
Dim intFileNumber As Integer = FreeFile()
FileOpen....
FileClose...
End Try

I would also recommend to use a FileStream or StreamWriter instead of
the that old stuff.

Dim fs As System.IO.FileStream
fs = File.Open....

Hope it helps
 
P

Phill W.

Stack Trace:

[IOException: Bad file name or number.]
Microsoft.VisualBasic.FileSystem.Write(Int32 FileNumber, Object[]
Output) +82
MyApp.PrinterSelectionForm.PrintMyReport() +1241
</exception>

Yet another example of where the Framework's "code-only" Stack Trace
lets us down. If the code's been broken by a bad file /number/, why
can't we see what the errant value is?
The code that throws this exception (much simplifed) is:

Public Sub PrintMyReport() .. . .
Dim intFileNumber As Integer = FreeFile()
Try
' A whole bunch of code to print a report using Crystal Reports.
' Crystal throws an exception

Catch ex As Exception
FileOpen(intFileNumber, "C:\My_Debug\MyAPP_ERRORS.txt",
OpenMode.Append)
.. . .

Could it be that you have /other/ file handling code, using FreeFile(),
elsewhere in the application and you're simply leaving one of /those/
files open? It's entirely possible that, after a while, FreeFile() will
no longer be able to return you a valid "file handle" and, when you try
to use it in your Exception Handler - Boom!

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top