Solved

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

Posted on 2009-07-14
5
894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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