Solved

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

Posted on 2009-07-14
5
892 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF and Powershell button hover change text 5 61
GUID's in SQL Server 4 30
VB Catch Statement in Event Viewer. 2 19
Difference between Highcharts and Mapbox 10 44
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)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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