Can VB.NET read file even if it is locked by another application?

Posted on 2006-04-15
Last Modified: 2010-04-23
Try this example.

1. Open a file in MS Word (Word locks the file).
2. Use 'Open With' and open the same file in Notepad (it works)
3. In VB.NET, I've tried File.ReadAllText, File.OpenRead and File.Open with read access and it says the file is locked.

If Notepad can open a file even if it is locked, surely there must be a way to do in VB.NET...

Does anyone know?
Question by:khooc
    1 Comment
    LVL 11

    Accepted Solution

    yer I got caught on this also..

    you need to allow the other application to write also to it so the share option is FileShare.ReadWrite.

    Dim oFS As IO.FileStream = Nothing
    Dim oFSt As IO.StreamReader = Nothing
    oFS = New IO.FileStream(sPath & sLogFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)  <--- FileShare.ReadWrite.
    oFSt = New IO.StreamReader(oFS)
    Dim s As String = oFSt.ReadLine


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    794 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

    17 Experts available now in Live!

    Get 1:1 Help Now