Esempio n. 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);
            }
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 4
0
        /// <summary>
        /// Get the last write time of the fullpath to the file.
        /// If the file does not exist, then DateTime.MinValue is returned
        /// </summary>
        /// <param name="fullPath">Full path to the file in the filesystem</param>
        /// <returns></returns>
        internal static DateTime GetLastWriteFileUtcTime(string fullPath)
        {
            DateTime fileModifiedTime      = DateTime.MinValue;
            WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA();
            bool success = false;

            success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);
            if (success)
            {
                long dt = ((long)(data.ftLastWriteTimeHigh) << 32) | ((long)data.ftLastWriteTimeLow);
                fileModifiedTime = DateTime.FromFileTimeUtc(dt);
            }

            return(fileModifiedTime);
        }
Esempio n. 5
0
        /// <summary>
        /// Get the last write time of the fullpath to the file.
        /// If the file does not exist, then DateTime.MinValue is returned
        /// </summary>
        /// <param name="fullPath">Full path to the file in the filesystem</param>
        /// <returns></returns>
        internal static DateTime GetLastWriteFileUtcTime(string fullPath)
        {
            DateTime fileModifiedTime = DateTime.MinValue;

            if (UseSymlinkTimeInsteadOfTargetTime)
            {
                WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA();
                bool success = false;

                success = NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref data);
                if (success)
                {
                    long dt = ((long)(data.ftLastWriteTimeHigh) << 32) | ((long)data.ftLastWriteTimeLow);
                    fileModifiedTime = DateTime.FromFileTimeUtc(dt);
                }
            }
            else
            {
                using (SafeFileHandle handle =
                           CreateFile(fullPath, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING,
                                      FILE_ATTRIBUTE_NORMAL, IntPtr.Zero))
                {
                    if (!handle.IsInvalid)
                    {
                        FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime;
                        if (!GetFileTime(handle, out ftCreationTime, out ftLastAccessTime, out ftLastWriteTime) != true)
                        {
                            long fileTime = ((long)(uint)ftLastWriteTime.dwHighDateTime) << 32 |
                                            (long)(uint)ftLastWriteTime.dwLowDateTime;
                            fileModifiedTime =
                                DateTime.FromFileTimeUtc(fileTime);
                        }
                    }
                }
            }

            return(fileModifiedTime);
        }
Esempio n. 6
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);
                }
            }
        }
 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));
 }