internal static int FillAttributeInfo(String path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound)
{
int errorCode = 0;
if (tryagain) // someone has a handle to the file open, or other error
{
Interop.Kernel32.WIN32_FIND_DATA findData;
findData = new Interop.Kernel32.WIN32_FIND_DATA();
// Remove trailing slash since this can cause grief to FindFirstFile. You will get an invalid argument error
String tempPath = path.TrimEnd(PathHelpers.DirectorySeparatorChars);
// For floppy drives, normally the OS will pop up a dialog saying
// there is no disk in drive A:, please insert one. We don't want that.
// SetErrorMode will let us disable this, but we should set the error
// mode back, since this may have wide-ranging effects.
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
try
{
bool error = false;
SafeFindHandle handle = Interop.Kernel32.FindFirstFile(tempPath, ref findData);
try
{
if (handle.IsInvalid)
{
error = true;
errorCode = Marshal.GetLastWin32Error();
if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND ||
errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND ||
errorCode == Interop.Errors.ERROR_NOT_READY) // floppy device not ready
{
if (!returnErrorOnNotFound)
{
// Return default value for backward compatibility
errorCode = 0;
data.fileAttributes = -1;
}
}
return errorCode;
}
}
finally
{
// Close the Win32 handle
try
{
handle.Dispose();
}
catch
{
// if we're already returning an error, don't throw another one.
if (!error)
{
throw Win32Marshal.GetExceptionForLastWin32Error();
}
}
}
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
}
// Copy the information to data
data.PopulateFrom(ref findData);
}
else
{
// For floppy drives, normally the OS will pop up a dialog saying
// there is no disk in drive A:, please insert one. We don't want that.
// SetErrorMode will let us disable this, but we should set the error
// mode back, since this may have wide-ranging effects.
bool success = false;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
try
{
success = Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data);
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
}
if (!success)
{
errorCode = Marshal.GetLastWin32Error();
if (errorCode != Interop.Errors.ERROR_FILE_NOT_FOUND &&
errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND &&
errorCode != Interop.Errors.ERROR_NOT_READY) // floppy device not ready
{
// In case someone latched onto the file. Take the perf hit only for failure
return FillAttributeInfo(path, ref data, true, returnErrorOnNotFound);
}
else
{
if (!returnErrorOnNotFound)
{
// Return default value for backward compatibility
errorCode = 0;
data.fileAttributes = -1;
}
}
}
}
return errorCode;
}