BUG: "Type System.IO.IOException" error message after you upgrade to Visual Basic .NET or Visual Basic 2005 (316155)



The information in this article applies to:

  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q316155

SYMPTOMS

After you upgrade your Microsoft Visual Basic 6 application to Visual Basic .NET or Visual Basic 2005, you may receive the following "exception" error message:
An unhandled exception of the type 'System.IO.IOException' occurred in microsoft.visualbasic.dll.

CAUSE

This behavior occurs when all of the following conditions are true:
  • The Visual Basic Upgrade Wizard upgrades Visual Basic 6 code that uses the Open statement to open a given file.

    -and-
  • You do not explicitly specify shared access to open this file.

    -and-
  • You attempt to open this file a second time without first closing it.
NOTE: You may also receive this error message if you try to open a given file a second time (without closing the first file handle) by using the FileOpen function in Visual Basic .NET or in Visual Basic 2005 without specifying the Share argument.

RESOLUTION

One of the parameters of the FileOpen method takes a parameter of type OpenShare. OpenShare is an enumeration. By passing OpenShare.Shared to FileOpen, you can open the file with shared access. This allows you to maintain more than one handle to an open file at a time. The following sample code uses FileOpen and OpenShare.Shared:
FileOpen(fn2, strFile, OpenMode.Input, , OpenShare.Shared)
				
NOTE: The documentation for the share parameter of the FileOpen method is incorrect.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Note The Visual Basic .NET or Visual Basic 2005 Upgrade Wizard is included in Visual Studio .NET Professional or in Visual Studio 2005.

In Visual Basic 6, you use the Open statement to open files for input and output. There are several modes you can use to open a file, including the following: Append, Binary, Input, Output, and Random. If you are opening a file for Binary, Input, or Random access, the file is implicitly shared, and you can include multiple handles to the file in your code.

When the Open statement is run through the Visual Basic Upgrade Wizard, the wizard modifies the code to use the FileOpen method of the Microsoft.VisualBasic.FileSystem class. This class is located in the Visual Basic Compatibility Library, which is located in the Microsoft.VisualBasic.dll assembly. The FileOpen method takes an optional parameter called Share. You must explicitly pass this parameter if you want to open the file for shared access. The code conversion done by the Upgrade Wizard does not do this for you.

Steps to Reproduce the Problem

  1. Create a new Visual Basic 6 Standard EXE application.
  2. Add a module to the project.
  3. Paste the following code into the module:
    Sub Main()
        Dim fn1 As Integer
        Dim fn2 As Integer
        
        fn1 = FreeFile()
        Open "C:\temp\test.txt" For Input As #fn1
        fn2 = FreeFile()
        Open "C:\temp\test.txt" For Input As #fn2
        Close fn1
        Close fn2
        MsgBox "Done"
    End Sub
    					
  4. You will need to either modify the code that you just pasted so that the path points to an existing file on your system (which you have access to), or you need to create the file and place it in the directory referenced in the code.
  5. In the project properties, change the startup object to Sub Main.
  6. Press F5 to run the project; verify that it works.

    You should receive a message box that displays "Done."
  7. Save the project, and then create the executable.
  8. Start Visual Studio .NET or Visual Studio 2005, and then open the Visual Basic project file that you just created. This will start the Upgrade Wizard. Select all of the default actions.
  9. After the Upgrade Wizard is finished, press F5 to run the project.

    Notice that you receive the "IOException" error message described in the "Symptoms" section of this article.

    Note If you want this code to work in Visual Basic .NET or in Visual Basic 2005, you will need to modify the calls to FileOpen so that you are passing OpenShare.Shared, as follows:
    Public Sub Main()
        Dim fn1 As Short
        Dim fn2 As Short
    
        fn1 = FreeFile()
            FileOpen(fn1, "C:\temp\test.txt", OpenMode.Input, , OpenShare.Shared)
        fn2 = FreeFile()
            FileOpen(fn2, "C:\temp\test.txt", OpenMode.Input, , OpenShare.Shared)
        FileClose(fn1)
        FileClose(fn2)
        MsgBox("Done")
    End Sub
    					

Modification Type:MajorLast Reviewed:1/27/2006
Keywords:kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbmigrate kbbug kberrmsg KB316155