Microsoft KB Archive/316155

= BUG: &quot;Type System.IO.IOException&quot; error message after you upgrade to Visual Basic .NET or Visual Basic 2005 =

Article ID: 316155

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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 &quot;exception&quot; 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 &quot;Applies to&quot; 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
 Create a new Visual Basic 6 Standard EXE application. Add a module to the project.  Paste the following code into the module: Sub Main Dim fn1 As Integer Dim fn2 As Integer fn1 = FreeFile Open &quot;C:\temp\test.txt&quot; For Input As #fn1 fn2 = FreeFile Open &quot;C:\temp\test.txt&quot; For Input As #fn2 Close fn1 Close fn2 MsgBox &quot;Done&quot; End Sub  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. In the project properties, change the startup object to Sub Main.</li> Press F5 to run the project; verify that it works.

You should receive a message box that displays &quot;Done.&quot;</li> Save the project, and then create the executable.</li> 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.</li>  After the Upgrade Wizard is finished, press F5 to run the project.

Notice that you receive the &quot;IOException&quot; error message described in the &quot;Symptoms&quot; 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, &quot;C:\temp\test.txt&quot;, OpenMode.Input,, OpenShare.Shared) fn2 = FreeFile FileOpen(fn2, &quot;C:\temp\test.txt&quot;, OpenMode.Input,, OpenShare.Shared) FileClose(fn1) FileClose(fn2) MsgBox(&quot;Done&quot;) End Sub </li></ol>

Keywords: kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbmigrate kbbug kberrmsg KB316155

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.