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. Check if File is in Use

Check if File is in Use

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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
    MumbleB
    wrote on last edited by
    #1

    Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.

    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)

    If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the file being used by another process, or something like that. Anybody have any idea? Thanks

    Excellence is doing ordinary things extraordinarily well.

    OriginalGriffO D L 3 Replies Last reply
    0
    • M MumbleB

      Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.

      FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)

      If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the file being used by another process, or something like that. Anybody have any idea? Thanks

      Excellence is doing ordinary things extraordinarily well.

      OriginalGriffO Online
      OriginalGriffO Online
      OriginalGriff
      wrote on last edited by
      #2

      I'm not sure you can. Notepad Opens the file, reads the content, and closes it again. It may never touch the file again, so how is the OS to know the file is "in use"? If you think about it, most well behaved applications do much the same - file handles are a scarce resource, so you don't hang onto them unless you really must.

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M MumbleB

        Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.

        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)

        If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the file being used by another process, or something like that. Anybody have any idea? Thanks

        Excellence is doing ordinary things extraordinarily well.

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

        There's no way to tell. Notepad doesn't hold the file open while you are viewing it. It's opened, read, and closed before you even see it on screen. Notepad also doesn't block other applications from reading the file at the same time it is, so this code will fail to detect Notepad using the file in all cases.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        • M MumbleB

          Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.

          FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)

          If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the file being used by another process, or something like that. Anybody have any idea? Thanks

          Excellence is doing ordinary things extraordinarily well.

          L Offline
          L Offline
          Lukasz Nowakowski
          wrote on last edited by
          #4

          Try opening this file in MS Word. It locks the file when you open it. Then when you open file, test your application.

          Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

          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