diff options
Diffstat (limited to 'src/mscorlib/src/System/IO/File.cs')
-rw-r--r-- | src/mscorlib/src/System/IO/File.cs | 81 |
1 files changed, 49 insertions, 32 deletions
diff --git a/src/mscorlib/src/System/IO/File.cs b/src/mscorlib/src/System/IO/File.cs index 7cc3f431a9..4aba1488ec 100644 --- a/src/mscorlib/src/System/IO/File.cs +++ b/src/mscorlib/src/System/IO/File.cs @@ -74,27 +74,30 @@ namespace System.IO return false; } - internal static bool InternalExists(String path) { + internal static bool InternalExists(String path) + { Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); int dataInitialised = FillAttributeInfo(path, ref data, false, true); - return (dataInitialised == 0) && (data.fileAttributes != -1) - && ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0); + return (dataInitialised == 0) && (data.fileAttributes != -1) + && ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0); } public static byte[] ReadAllBytes(String path) { byte[] bytes; - using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, - FileStream.DefaultBufferSize, FileOptions.None)) { + using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, + FileStream.DefaultBufferSize, FileOptions.None)) + { // Do a blocking read int index = 0; long fileLength = fs.Length; if (fileLength > Int32.MaxValue) - throw new IOException(Environment.GetResourceString("IO.IO_FileTooLong2GB")); - int count = (int) fileLength; + throw new IOException(SR.IO_FileTooLong2GB); + int count = (int)fileLength; bytes = new byte[count]; - while(count > 0) { + while (count > 0) + { int n = fs.Read(bytes, index, count); if (n == 0) __Error.EndOfFile(); @@ -111,7 +114,7 @@ namespace System.IO if (path == null) throw new ArgumentNullException(nameof(path)); if (path.Length == 0) - throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath")); + throw new ArgumentException(SR.Argument_EmptyPath); Contract.EndContractBlock(); return InternalReadAllLines(path, Encoding.UTF8); @@ -133,7 +136,7 @@ namespace System.IO return lines.ToArray(); } #endif // PLATFORM_UNIX - + // Returns 0 on success, otherwise a Win32 error code. Note that // classes should use -1 as the uninitialized state for dataInitialized. internal static int FillAttributeInfo(String path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound) @@ -142,29 +145,33 @@ namespace System.IO if (tryagain) // someone has a handle to the file open, or other error { Win32Native.WIN32_FIND_DATA findData; - findData = new Win32Native.WIN32_FIND_DATA (); - + findData = new Win32Native.WIN32_FIND_DATA(); + // Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error - String tempPath = path.TrimEnd(new char [] {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}); + String tempPath = path.TrimEnd(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); // 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. int oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); - try { + try + { bool error = false; - SafeFindHandle handle = Win32Native.FindFirstFile(tempPath,findData); - try { - if (handle.IsInvalid) { + SafeFindHandle handle = Win32Native.FindFirstFile(tempPath, findData); + try + { + if (handle.IsInvalid) + { error = true; dataInitialised = Marshal.GetLastWin32Error(); - + if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND || dataInitialised == Win32Native.ERROR_PATH_NOT_FOUND || dataInitialised == Win32Native.ERROR_NOT_READY) // floppy device not ready { - if (!returnErrorOnNotFound) { + if (!returnErrorOnNotFound) + { // Return default value for backward compatibility dataInitialised = 0; data.fileAttributes = -1; @@ -173,21 +180,26 @@ namespace System.IO return dataInitialised; } } - finally { + finally + { // Close the Win32 handle - try { + try + { handle.Close(); } - catch { + catch + { // if we're already returning an error, don't throw another one. - if (!error) { + if (!error) + { Debug.Assert(false, "File::FillAttributeInfo - FindClose failed!"); __Error.WinIOError(); } } } } - finally { + finally + { Win32Native.SetErrorMode(oldMode); } @@ -195,31 +207,36 @@ namespace System.IO data.PopulateFrom(findData); } else - { - // For floppy drives, normally the OS will pop up a dialog saying + { + // 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; int oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); - try { + try + { success = Win32Native.GetFileAttributesEx(path, GetFileExInfoStandard, ref data); } - finally { + finally + { Win32Native.SetErrorMode(oldMode); } - if (!success) { + if (!success) + { dataInitialised = Marshal.GetLastWin32Error(); if (dataInitialised != Win32Native.ERROR_FILE_NOT_FOUND && dataInitialised != Win32Native.ERROR_PATH_NOT_FOUND && dataInitialised != Win32Native.ERROR_NOT_READY) // floppy device not ready { - // In case someone latched onto the file. Take the perf hit only for failure + // In case someone latched onto the file. Take the perf hit only for failure return FillAttributeInfo(path, ref data, true, returnErrorOnNotFound); } - else { - if (!returnErrorOnNotFound) { + else + { + if (!returnErrorOnNotFound) + { // Return default value for backward compbatibility dataInitialised = 0; data.fileAttributes = -1; |