Example #1
0
        /// <summary>
        /// Returns if the directory exists
        /// </summary>
        /// <param name="fullPath">Full path to the file in the filesystem</param>
        /// <returns></returns>
        internal static bool FileExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            if (NativeMethodsShared.IsWindows)
            {
                NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
                bool success = false;

                success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);
                if (success)
                {
                    return((data.fileAttributes & NativeMethodsShared.FILE_ATTRIBUTE_DIRECTORY) == 0);
                }

                return(false);
            }

            try
            {
                return(File.Exists(fullPath));
            }
            catch
            {
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// If there is a directory or file at the specified path, returns true.
        /// Otherwise, returns false.
        /// Does not throw IO exceptions, to match Directory.Exists and File.Exists.
        /// Unlike calling each of those in turn it only accesses the disk once, which is faster.
        /// </summary>
        internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);

            NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            bool success = false;

            success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);

            return(success);
        }
Example #3
0
        /// <summary>
        /// Returns if the directory exists
        /// </summary>
        /// <param name="fullPath">Full path to the directory in the filesystem</param>
        /// <returns></returns>
        internal static bool DirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);

            NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            bool success = false;

            success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);
            if (success)
            {
                return((data.fileAttributes & NativeMethodsShared.FILE_ATTRIBUTE_DIRECTORY) != 0);
            }

            return(false);
        }
Example #4
0
        /// <summary>
        /// If there is a directory or file at the specified path, returns true.
        /// Otherwise, returns false.
        /// Does not throw IO exceptions, to match Directory.Exists and File.Exists.
        /// Unlike calling each of those in turn it only accesses the disk once, which is faster.
        /// </summary>
        internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            if (NativeMethodsShared.IsWindows)
            {
                NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
                bool success = false;

                success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);

                return(success);
            }
            else
            {
                try
                {
                    return(File.Exists(fullPath) || Directory.Exists(fullPath));
                }
                catch
                {
                    return(false);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Ensure we have the data.
        /// Does not throw for nonexistence.
        /// </summary>
        private void EnsurePopulated()
        {
            if (_dataIsGood == null)
            {
                _dataIsGood = false;
                _filename = FileUtilities.AttemptToShortenPath(_filename); // This is no-op unless the path actually is too long
                _data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();

                // THIS COPIED FROM THE BCL:
                //
                // 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 = NativeMethodsShared.SetErrorMode(1 /* ErrorModes.SEM_FAILCRITICALERRORS */);

                bool success = false;
                _fileOrDirectoryExists = true;

                try
                {
                    success = NativeMethodsShared.GetFileAttributesEx(_filename, 0, ref _data);

                    if (!success)
                    {
                        int error = Marshal.GetLastWin32Error();

                        // File not found is the most common case, for example we're copying
                        // somewhere without a file yet. Don't do something like FileInfo.Exists to
                        // get a nice error, or we're doing IO again! Don't even format our own string:
                        // that turns out to be unacceptably expensive here as well. Set a flag for this particular case.
                        //
                        // Also, when not under debugger (!) it will give error == 3 for path too long. Make that consistently throw instead.
                        if ((error == 2 /* ERROR_FILE_NOT_FOUND */ || error == 3 /* ERROR_PATH_NOT_FOUND */) &&
                            _filename.Length <= NativeMethodsShared.MAX_PATH)
                        {
                            _fileOrDirectoryExists = false;
                            return;
                        }

                        // Throw nice message as far as we can. At this point IO is OK.
                        var length = new FileInfo(_filename).Length;

                        // Otherwise this will give at least something
                        NativeMethodsShared.ThrowExceptionForErrorCode(error);
                        ErrorUtilities.ThrowInternalErrorUnreachable();
                    }
                }
                finally
                {
                    NativeMethodsShared.SetErrorMode(oldMode);
                }

                _dataIsGood = true;
            }
        }
Example #6
0
 /// <summary>
 /// Use in case the state is known to have changed exogenously.
 /// </summary>
 internal void Reset()
 {
     _data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
     _dataIsGood = null;
 }
Example #7
0
        /// <summary>
        /// If there is a directory or file at the specified path, returns true.
        /// Otherwise, returns false.
        /// Does not throw IO exceptions, to match Directory.Exists and File.Exists.
        /// Unlike calling each of those in turn it only accesses the disk once, which is faster.
        /// </summary>
        internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);

            NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            bool success = false;

            success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);

            return success;
        }
Example #8
0
        /// <summary>
        /// Returns if the directory exists
        /// </summary>
        /// <param name="fullPath">Full path to the file in the filesystem</param>
        /// <returns></returns>
        internal static bool FileExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);

            NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA data = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            bool success = false;

            success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);
            if (success)
            {
                return ((data.fileAttributes & NativeMethodsShared.FILE_ATTRIBUTE_DIRECTORY) == 0);
            }

            return false;
        }
 internal static bool DirectoryExistsNoThrow(string fullPath)
 {
     fullPath = AttemptToShortenPath(fullPath);
     NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
     return (NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) != 0));
 }
 internal static bool DirectoryExistsNoThrow(string fullPath)
 {
     fullPath = AttemptToShortenPath(fullPath);
     NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
     return(NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) != 0));
 }