Checking for object lock

I am executing some C# code to initiate the creation of a file on our host system (AS/400). Once the file has been created on the AS/400, the AS/400 "pushes" the file over to the Windows server where my C# code is running.
I need to be able to wait until the file is completely over to the Windows server from the AS/400 host before exiting the thread. I would like to set up a while loop to check to see if the object exists and if it is locked. If it does exist and it's not locked, then I should be able to assume it has arrived in the folder on my Windows Server.
Any help would be greatly appreciated!
Who is Participating?
Michael FowlerSolutions ConsultantCommented:
Why not just try opening the file

While (True) {
   try {
      FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None)
   Catch (exception) { System.Threading.Thread.Sleep(500); }
   Finally { fileStream.Close(); }

Open in new window

Just tweak the sleep time to suit your application

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.