Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Access Denied trying to read file with StreamReader [modified]

Access Denied trying to read file with StreamReader [modified]

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Casto
    wrote on last edited by
    #1

    I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006

    D M 2 Replies Last reply
    0
    • M Matt Casto

      I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If the process that has the file open for write is using DenyShareRead or DennyShareAll to open it, then you can't open it for reading at all until the service closes the file.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        If the process that has the file open for write is using DenyShareRead or DennyShareAll to open it, then you can't open it for reading at all until the service closes the file.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        M Offline
        M Offline
        Matt Casto
        wrote on last edited by
        #3

        Ouch. In that scenario, would it be possible to read the file using WinAPI?

        D 1 Reply Last reply
        0
        • M Matt Casto

          I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006

          M Offline
          M Offline
          Matt Casto
          wrote on last edited by
          #4

          I solved this problem by using a FileStream instead of a StreamReader. The constructor of the FileStream allows you to set FileAccess and FileShare which avoids the access problems. The FileStream doesn't allow you to read line-by-line nicely, but I happen to have a class that I re-use often which acts like a TextReader but uses a FileStream in the background.

          D 1 Reply Last reply
          0
          • M Matt Casto

            Ouch. In that scenario, would it be possible to read the file using WinAPI?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Matt Casto wrote:

            would it be possible to read the file using WinAPI?

            No, because it's the Win API that is underlying the Stream classes.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            1 Reply Last reply
            0
            • M Matt Casto

              I solved this problem by using a FileStream instead of a StreamReader. The constructor of the FileStream allows you to set FileAccess and FileShare which avoids the access problems. The FileStream doesn't allow you to read line-by-line nicely, but I happen to have a class that I re-use often which acts like a TextReader but uses a FileStream in the background.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              You can wrap the FileStream in a StreamReader object like this:

              FileStream fs = New FileStream("filepath", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
              StreamReader sr = New StreamReader(fs);

              You'll then have all the advantages of the StreamReader, with the access flexibility of the FileStream. -- modified at 17:47 Friday 18th August, 2006

              Dave Kreskowiak Microsoft MVP - Visual Basic

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups