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. Get file size - while inuse by another process

Get file size - while inuse by another process

Scheduled Pinned Locked Moved C#
help
6 Posts 3 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.
  • L Offline
    L Offline
    liqnit
    wrote on last edited by
    #1

    Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck. string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); } Any idea could help.:confused::confused:

    Have a nice Day

    M N 2 Replies Last reply
    0
    • L liqnit

      Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck. string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); } Any idea could help.:confused::confused:

      Have a nice Day

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! One idea that's working fine in some of my programs is trying to open the target file exclusively (with FileShare.None). If the file can be opened, no other process is accessing the file and thus the file has been created completely. If you get an IOException then sleep for a few ms and try again. Just don't forget to close the file afterwards and include a timeout mechanism so your program doesn't lock up if the file isn't closed because of a bug in the other application.

      Regards, mav -- Black holes are the places where God divided by 0...

      L 1 Reply Last reply
      0
      • M mav northwind

        Hi! One idea that's working fine in some of my programs is trying to open the target file exclusively (with FileShare.None). If the file can be opened, no other process is accessing the file and thus the file has been created completely. If you get an IOException then sleep for a few ms and try again. Just don't forget to close the file afterwards and include a timeout mechanism so your program doesn't lock up if the file isn't closed because of a bug in the other application.

        Regards, mav -- Black holes are the places where God divided by 0...

        L Offline
        L Offline
        liqnit
        wrote on last edited by
        #3

        Thanks for the quick reply. I understand your suggestion but the problem is that the process holding the file is loading a message box and until i press "ok" the file is still open by it. The messagebox is not always present . I want to get a message when the file is not updated anymore .

        Have a nice Day

        M 1 Reply Last reply
        0
        • L liqnit

          Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck. string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); } Any idea could help.:confused::confused:

          Have a nice Day

          N Offline
          N Offline
          Nissim Salomon
          wrote on last edited by
          #4

          Hi use the FileSystemWatcher class

          L 1 Reply Last reply
          0
          • L liqnit

            Thanks for the quick reply. I understand your suggestion but the problem is that the process holding the file is loading a message box and until i press "ok" the file is still open by it. The messagebox is not always present . I want to get a message when the file is not updated anymore .

            Have a nice Day

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            I fear you're out of luck in this case. As long as the file is held open by the other application, this other application is free to modify the contents of the file at any given time. So you can never be sure that the other app will not write anything to the file. Depending on the architecture of the other app it could very well be that a background thread still writes to the file even though a message box is being displayed.

            Regards, mav -- Black holes are the places where God divided by 0...

            1 Reply Last reply
            0
            • N Nissim Salomon

              Hi use the FileSystemWatcher class

              L Offline
              L Offline
              liqnit
              wrote on last edited by
              #6

              Thanks a lot i will try

              Have a nice Day

              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