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. Reading a used file

Reading a used file

Scheduled Pinned Locked Moved C#
helpcsharpquestion
5 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.
  • J Offline
    J Offline
    j hannemann
    wrote on last edited by
    #1

    Hi. I have a batch-file that creates a logfile. My C# Programm should read this file even if this will still be updated. The (wonderful) editor "Textpad" can easily open that file ... my C# Program can't .. I get 'file is used by another process' Error. I already tried : FileStream output = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read, 4196, true); but I still get the error. Can anyone help me PLEASE ?? :confused:

    E 1 Reply Last reply
    0
    • J j hannemann

      Hi. I have a batch-file that creates a logfile. My C# Programm should read this file even if this will still be updated. The (wonderful) editor "Textpad" can easily open that file ... my C# Program can't .. I get 'file is used by another process' Error. I already tried : FileStream output = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read, 4196, true); but I still get the error. Can anyone help me PLEASE ?? :confused:

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      Try using: File myFile = File.Open(...) using the version that takes a FileShare enumeration, and then create the FileStream from that.

      J 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        Try using: File myFile = File.Open(...) using the version that takes a FileShare enumeration, and then create the FileStream from that.

        J Offline
        J Offline
        j hannemann
        wrote on last edited by
        #3

        Hi Eric. I tried, like you said --- but this results in an error (File.Open returns a FileStream !). He can't convert from FileStream to File .. so I assumed you meant : FileStream myfile = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read); But this also causes the exception as before. Or did I misunderstood you ?

        E 1 Reply Last reply
        0
        • J j hannemann

          Hi Eric. I tried, like you said --- but this results in an error (File.Open returns a FileStream !). He can't convert from FileStream to File .. so I assumed you meant : FileStream myfile = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read); But this also causes the exception as before. Or did I misunderstood you ?

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          When you specify sharing, you need to match the sharing that the file was opened with. In my tests, using FileShare.ReadWrite worked.

          J 1 Reply Last reply
          0
          • E Eric Gunnerson msft

            When you specify sharing, you need to match the sharing that the file was opened with. In my tests, using FileShare.ReadWrite worked.

            J Offline
            J Offline
            j hannemann
            wrote on last edited by
            #5

            Like you said : It works ! I misunderstood the FileShare .. I thought that was for my access. Thank you very much !

            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