?
Solved

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

Posted on 2009-07-14
5
Medium Priority
?
900 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 1000 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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