Here is the code that I use for that purpose. Worked for me so far 
//
// Check if a local file or dir exists (via open)
//
BOOL CheckElementExistsViaOpen( PUNICODE_STRING puPath )
{
DBGASSERTIRQLEQUAL(PASSIVE_LEVEL);
IO_STATUS_BLOCK IoStatus;
NTSTATUS status = GetExistanceStatus( puPath, &IoStatus );
if( NT_SUCCESS(status) )
{
return TRUE;
}
else if( (status == STATUS_OBJECT_NAME_NOT_FOUND) ||
(IoStatus.Information == FILE_DOES_NOT_EXIST) ||
(status == STATUS_OBJECT_PATH_NOT_FOUND) ||
(status == STATUS_OBJECT_NAME_INVALID) ||
(status == STATUS_OBJECT_PATH_INVALID) )
{
return FALSE;
}
return TRUE;
}
//
// Get element’s existance status
//
NTSTATUS GetExistanceStatus( PUNICODE_STRING puPath, PIO_STATUS_BLOCK
pIoStatus )
{
DBGASSERTIRQLEQUAL(PASSIVE_LEVEL);
NTSTATUS status;
OBJECT_ATTRIBUTES aFileAttrib;
FILE_NETWORK_OPEN_INFORMATION aInfo;
#if (WINVER>=0x500)
InitializeObjectAttributes( &aFileAttrib, puPath,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL );
#else
InitializeObjectAttributes( &aFileAttrib, puPath,
OBJ_CASE_INSENSITIVE, NULL, NULL );
#endif
//
// Use FastOpen (if possible). If not that call will roll
// create IRP.
//
status = IoFastQueryNetworkAttributes( &aFileAttrib, SYNCHRONIZE, 0,
pIoStatus, &aInfo);
if( NT_SUCCESS(status) )
{
status = pIoStatus -> Status;
}
return status;
}
-----Original Message-----
From: Alexei Jelvis [mailto:xxxxx@rogers.com]
Sent: Wednesday, April 30, 2003 7:59 AM
To: File Systems Developers
Subject: [ntfsd] Re: Determine file existence
I need to determine file existence in my driver. I tried using
ZwCreateFile in order to find out, but some time the file may be opened
(by another process) using exclusive access, so ZwCreateFile will fail.
You can distinguish this by analyzing status returned by ZwCreateFile.
Alternatively you can build IRP_MJ_DIRECTORY_CONTROL and send it to the
directory where the file is located.
What is the best (100% safe) way to determine file existence?
There is no 100% safe way in general, file may be deleted/created/renamed
after you have tested it for existence but before you performed some
actions based on information from your test. What need to be done depends
on actual requirements that you have.
Alexei.
You are currently subscribed to ntfsd as: xxxxx@borland.com
To unsubscribe send a blank email to xxxxx@lists.osr.com