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. How to know if a file is being used by another process?

How to know if a file is being used by another process?

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

    Hello frenz, In my application, I need to recreate & save an xml file frequently in a thread. I use the Save() of the XmlDocument class to do this. But at times, when I try to save the file, I get an WIn32IOError exception saying that the file is being used by another process.. Is it possible to check whether the file is being used by another program before saving? If it is being used by another process, I need my application to wait until the resource is released to rewrite it. Advance thanks for the help.

    J S M 3 Replies Last reply
    0
    • J jamesjk

      Hello frenz, In my application, I need to recreate & save an xml file frequently in a thread. I use the Save() of the XmlDocument class to do this. But at times, when I try to save the file, I get an WIn32IOError exception saying that the file is being used by another process.. Is it possible to check whether the file is being used by another program before saving? If it is being used by another process, I need my application to wait until the resource is released to rewrite it. Advance thanks for the help.

      J Offline
      J Offline
      jdkulkarni
      wrote on last edited by
      #2

      Try this.. File.GetLastAccessTime(@"C:\time.txt"); It will give last access time of the given file. If it is say within some given time limit, you can check it and then wait using Thread.Sleep().. and then continue..:)

      Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

      1 Reply Last reply
      0
      • J jamesjk

        Hello frenz, In my application, I need to recreate & save an xml file frequently in a thread. I use the Save() of the XmlDocument class to do this. But at times, when I try to save the file, I get an WIn32IOError exception saying that the file is being used by another process.. Is it possible to check whether the file is being used by another program before saving? If it is being used by another process, I need my application to wait until the resource is released to rewrite it. Advance thanks for the help.

        S Offline
        S Offline
        Shajeel
        wrote on last edited by
        #3

        u can use this logic as well DateTime StartTime = DateTime.Now; while(true) { try { //save file } catch { if(StartTime.AddSeconds(timeouttime) > DateTime.Now) { //timeout expired break; } System.Threading.Thread.Sleep(sleeptime); continue; } break; }

        Regards Shajeel

        1 Reply Last reply
        0
        • J jamesjk

          Hello frenz, In my application, I need to recreate & save an xml file frequently in a thread. I use the Save() of the XmlDocument class to do this. But at times, when I try to save the file, I get an WIn32IOError exception saying that the file is being used by another process.. Is it possible to check whether the file is being used by another program before saving? If it is being used by another process, I need my application to wait until the resource is released to rewrite it. Advance thanks for the help.

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, I gave this suggestion some time ago: http://www.codeproject.com/script/comments/forums.asp?msg=1782792&forumid=1649&XtraIDs=1649&searchkw=Readonly&sd=22+Sep+2006&ed=21+Dec+2006&author=Martin%23&stype=1#xx1782792xx[^] I got now answer if it works, maybe you can try it. If you are using a "while" statement for waiting, be sure that you have some last exit possibility! All the best, Martin

          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