views:

85

answers:

3

How to identify the file has been created completely in c#..

Which command do we need to use to see that the file has been created completely.. some large file takes time to create completely how to identify them..

Thanks

A: 

The only method I know of is to try opening it to see if another process still has a hold on it, e.g.:

 private static Boolean FileInUse(FileInfo file)
 {
  Boolean inUse = false;
  try
  {
   using (file.OpenRead()) {}
  }
  catch (Exception exception)
  {
   inUse = true;
  }

  return inUse;
 }
marijne
Depending on the sharing options for opening files this may succeed even if another process is still writing to the file.
Joey
-1: This code will successfully open files which are still opened by other processes, but not locked for reading. Lots of applications do that. Some of them even allow other applications to write into the file while they're writing.
Rom
i need to display the created file on my UI, but i get create file notification as soon as file created,but it takes some time to write to file completely, till that time i am waiting in loop, but this code works fine but not for all cases, some time though the file not created it will give read access i mean at the end of file. means when the file write above to complete.. do u have any idea of how to lock a fine.. till write gets compleated.
Shadow
A: 

See similar discussion:

http://stackoverflow.com/questions/30074/monitoring-files-how-to-know-when-a-file-is-complete

Matt Lacey
can u tell me, how to lock file in c#.please
Shadow
Define what you mean by 'lock'
Matt Lacey
A: 
private static Boolean FileInUse(FileInfo file)
        {
                Boolean inUse = false;
                try
                {
                        using (file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {}
                }
                catch (Exception exception)
                {
                        inUse = true;
                }

                return inUse;
        }
FractalizeR
That seems a bit rough - what about all the other possible reasons that FileInfo.Open() could fail. They certainly do NOT all mean that the file is "in use".
Christian.K
Thanks dude,your code worked
Shadow
Christian.K is right. Grabit might need to check exception object to find out is there a sharing violation or some other problem. But I think the solution should still work.
FractalizeR
yes,But. its working FractalizeR
Shadow