Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
File.Exists check
Message
 
 
To
All
General information
Forum:
C#
Category:
Coding, syntax and commands
Title:
File.Exists check
Miscellaneous
Thread ID:
01677467
Message ID:
01677467
Views:
48
Hi everybody,

I'm wondering if anyone knows if File.Exists check is reliable to process a file? We have a situation when we get an error attempting to read a big file which is not yet fully written. Windows Explorer shows file creation date as one time and then about 30 seconds later shows last modified date and the error occurs in between these 2 times, e.g.
Filename	Created	Error	Modified
File1.TXT	1:45:05	1:45:23	1:45:35
File2.TXT	1:44:46	1:45:23	1:45:58
File3.TXT	1:45:18	1:45:23	1:45:27
File4.TXT	1:45:20	1:45:23	1:45:27
I'm wondering if the code like this (right now is using some hard-coded numbers, but I'm changing it to database settings as well) going to be sufficient to fix the problem:
internal bool FileIsReadyToProcess(string fullPathFileName)
        {           
            int waitForFileToCopy = 500; // wait 0.5 second
            int iCount = 0;
            bool isValid = false;
            do
            {
                isValid = File.Exists(fullPathFileName);
                if (!isValid) Thread.Sleep(waitForFileToCopy);
            } while (!isValid && iCount++ < 10);
            return isValid;
        }
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform