[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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