Solved

VB.NET--FileOpen--The process cannot access the file because it is in use

Posted on 2009-07-14
5
881 Views
Last Modified: 2012-05-07
Consider a CSV file open within Microsoft Excel.

VB6 allows opening the file for Read Only access.  VB.NET does not.  VB.NET generates the following error message:

"The process cannot access the file because it is in use . . . "

Please can anyone find a workaround?

Below is VB6 code which works just fine, followed by VB.NET code which does not.  (I've tried using other OpenMode options, using a FileStream, and using FileShared IO options; they were no help at all).

VB6:

     Dim iFNum As Integer
         
     iFNum = FreeFile()
     
     Open "C:\any file.csv" For Input As #iFNum


VB.NET

    Dim iFNum As Integer

    iFNum = FreeFile()

    FileOpen(iFNum, sFn, OpenMode.Input)
0
Comment
Question by:richelieu7777
  • 3
5 Comments
 

Author Comment

by:richelieu7777
ID: 24851323
correction to my original post:

replace
"FileOpen(iFNum, sFn, OpenMode.Input)"
with
"FileOpen(iFNum, "C:\any file.csv", OpenMode.Input)"
0
 

Author Comment

by:richelieu7777
ID: 24851362
The fact that VB.NET doesn't allow an open file Read Only access makes no sense to me.  For instance, you can *copy* an open file via the:

      File.Copy( "C:\any file.csv",  "C:\any file-TEMP.csv", True)

but you cannot read it.  One possible work-around would be to make a copy of each file you want Read Only access to, then open it for reading, but this should be unnecessary, and would be a highly undesirable solution especially for large files -- the CPU time for the disk copy command may be prohibitively long.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24852109
Does this work?

        Dim file As String = "C:\any file.csv"
        Dim fs As New FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Dim sr As New StreamReader(fs)
        Dim line As String
        While Not sr.EndOfStream
            line = sr.ReadLine
            Debug.Print(line)
        End While
        sr.Close()
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 24852545
You have to specify more options to indicate things like how you want the file locked (locked for read, locked for write, or none) and what sort of Sharing you want to do.  Try this, it worked for me...

FileOpen(iFNum, "C:\Test.csv", OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
0
 

Author Closing Comment

by:richelieu7777
ID: 31603339
Both solutions worked well.  THANKS!!!!!!!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now