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. File sharing query

File sharing query

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
3 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.
  • B Offline
    B Offline
    benglish72
    wrote on last edited by
    #1

    Hi, I'm writing a program that attempts to open a file stream using FileInfo.OpenRead(), this seems to fail when another application is accessing the same file. As I need to be able to read this file whilst another has it open or access it, is there anyway I can get it to open instead of throwing IOException in C#? Thanks. Here's a code snippet //check file exists System.IO.FileInfo info = new System.IO.FileInfo(sFileName); if(info!=null&&info.Exists==true) { //ok, file exits, lets read it in... try { System.IO.Stream stream = info.OpenRead(); //do stuff here stream.Close(); } catch(IOException exception) { }

    D 1 Reply Last reply
    0
    • B benglish72

      Hi, I'm writing a program that attempts to open a file stream using FileInfo.OpenRead(), this seems to fail when another application is accessing the same file. As I need to be able to read this file whilst another has it open or access it, is there anyway I can get it to open instead of throwing IOException in C#? Thanks. Here's a code snippet //check file exists System.IO.FileInfo info = new System.IO.FileInfo(sFileName); if(info!=null&&info.Exists==true) { //ok, file exits, lets read it in... try { System.IO.Stream stream = info.OpenRead(); //do stuff here stream.Close(); } catch(IOException exception) { }

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

      If the other app has the file opened DenyShareRead at the least, you're screwed. The other problem is that you have to request that the file be opened with Shared Read/Write access. If the other open opens and closes the file frequently, you have to open the file with Shared access so the other app won't fail when it tries to open the file. The FileOpen method does not do this. Instead, you'll have to use the FileStream class to open the file:

      FileStream myFileStream = New FileStream("C:\myFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

      Now, the rub is that you can request Shared access to the file, but it only takes effect for subsequent requests by other processes (apps) to open the file. It will NOT give you access to a file that is opened and locked by another process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      B 1 Reply Last reply
      0
      • D Dave Kreskowiak

        If the other app has the file opened DenyShareRead at the least, you're screwed. The other problem is that you have to request that the file be opened with Shared Read/Write access. If the other open opens and closes the file frequently, you have to open the file with Shared access so the other app won't fail when it tries to open the file. The FileOpen method does not do this. Instead, you'll have to use the FileStream class to open the file:

        FileStream myFileStream = New FileStream("C:\myFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        Now, the rub is that you can request Shared access to the file, but it only takes effect for subsequent requests by other processes (apps) to open the file. It will NOT give you access to a file that is opened and locked by another process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        B Offline
        B Offline
        benglish72
        wrote on last edited by
        #3

        :-D Hey Dave, thanks, that worked a treat! Good stuff.

        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