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