public static bool Exists(string path)
        {
#if DOTNET5_4
            string longFilePath = path;
            if (Interop.CrossPlatformHelpers.IsWindows)
            {
                longFilePath = LongPath.ToUncPath(longFilePath);
            }
            return(File.Exists(longFilePath));
#else
            try
            {
                if (String.IsNullOrEmpty(path))
                {
                    return(false);
                }
                path = LongPath.ToUncPath(path);
                bool success = NativeMethods.PathFileExistsW(path);
                if (!success)
                {
                    NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] { 0, NativeMethods.ERROR_DIRECTORY_NOT_FOUND, NativeMethods.ERROR_FILE_NOT_FOUND });
                }
                var fileAttributes = GetAttributes(path);
                return(success && (FileAttributes.Directory != (fileAttributes & FileAttributes.Directory)));
            }
            catch (ArgumentException) { }
            catch (NotSupportedException) { }  // Security can throw this on ":"
            catch (System.Security.SecurityException) { }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }

            return(false);
#endif
        }
 public void SetDirectoryPath(string directoryPath)
 {
     if (directoryPath != null)
     {
         this.FilePath = LongPath.Combine(directoryPath, this.RelativePath);
     }
 }
        public static string ToUncPath(string path)
        {
            if (IsDevice(path))
            {
                return(LongPath.GetFullPath(path));
            }

            if (IsPartiallyQualified(path))
            {
                path = LongPath.GetFullPath(path);
                if (IsDevice(path))
                {
                    return(path);
                }
                else
                {
                    return(ExtendedPathPrefix + path);
                }
            }

            //// Given \\server\share in longpath becomes \\?\UNC\server\share
            if (path.StartsWith(UncPathPrefix, StringComparison.OrdinalIgnoreCase))
            {
                return(LongPath.GetFullPath(path.Insert(2, UncExtendedPrefixToInsert)));
            }

            return(LongPath.GetFullPath(ExtendedPathPrefix + path));
        }
        private static void CreateParentDirectoryIfNotExists(string path)
        {
            string dir = LongPath.GetDirectoryName(path);

            if (!string.IsNullOrEmpty(dir) && !LongPathDirectory.Exists(dir))
            {
                LongPathDirectory.CreateDirectory(dir);
            }
        }
        public static FileStream Open(string filePath, FileMode mode, FileAccess access, FileShare share)
        {
#if DOTNET5_4
            return(new FileStream(filePath, mode, access, share));
#else
            filePath = LongPath.ToUncPath(filePath);
            SafeFileHandle fileHandle = GetFileHandle(filePath, mode, access, share);
            return(new FileStream(fileHandle, access));
#endif
        }
        private FileLocation(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
            {
                throw new System.ArgumentNullException("info");
            }

            if (context.Context is StreamJournal &&
                "RelativePath".Equals(info.GetString(FilePathType)))
            {
                this.RelativePath = info.GetString(FilePathName);
                this.FilePath     = LongPath.Combine(((StreamJournal)context.Context).DirectoryPath, this.RelativePath);
            }
            else
            {
                this.RelativePath = null;
                this.FilePath     = info.GetString(FilePathName);
            }
        }
        /// <summary>
        /// Creates all directories and subdirectories in the specified path unless they already exist.
        /// </summary>
        /// <param name="path">The directory to create.</param>
        public static void CreateDirectory(string path)
        {
#if DOTNET5_4
            Directory.CreateDirectory(path);
#else
            var dir = LongPath.GetDirectoryName(path);
            if (!string.IsNullOrEmpty(dir) && !LongPathDirectory.Exists(dir))
            {
                LongPathDirectory.CreateDirectory(dir);
            }

            path = LongPath.ToUncPath(path);

            if (!NativeMethods.CreateDirectoryW(path, IntPtr.Zero))
            {
                NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] {
                    NativeMethods.ERROR_SUCCESS,
                    NativeMethods.ERROR_ALREADY_EXISTS
                });
            }
#endif
        }
        private static bool IsValidWindowsFileName(string fileName)
        {
            string fileNameNoExt   = LongPath.GetFileNameWithoutExtension(fileName);
            string fileNameWithExt = LongPath.GetFileName(fileName);

            if (Array.Exists <string>(ReservedBaseFileNames, delegate(string s) { return(fileNameNoExt.Equals(s, StringComparison.OrdinalIgnoreCase)); }))
            {
                return(false);
            }

            if (Array.Exists <string>(ReservedFileNames, delegate(string s) { return(fileNameWithExt.Equals(s, StringComparison.OrdinalIgnoreCase)); }))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(fileNameWithExt))
            {
                return(false);
            }

            bool allDotsOrWhiteSpace = true;

            for (int i = 0; i < fileName.Length; ++i)
            {
                if (fileName[i] != '.' && !char.IsWhiteSpace(fileName[i]))
                {
                    allDotsOrWhiteSpace = false;
                    break;
                }
            }

            if (allDotsOrWhiteSpace)
            {
                return(false);
            }

            return(true);
        }
        public static IEnumerable <string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption, FilesOrDirectory filter)
        {
#if DOTNET5_4
            return(Directory.EnumerateFileSystemEntries(path, searchPattern, searchOption));
#else
            NativeMethods.WIN32_FIND_DATA findData;
            NativeMethods.SafeFindHandle  findHandle;
            string currentPath = null;
            int    errorCode   = 0;

            Queue <string> folders    = new Queue <string>();
            String         searchPath = LongPath.Combine(path, searchPattern);
            path          = LongPath.GetDirectoryName(searchPath);
            searchPattern = LongPath.GetFileName(searchPath);
            folders.Enqueue(path);
            while (folders.Count > 0)
            {
                currentPath = folders.Dequeue();
                if (searchOption == SearchOption.AllDirectories)
                {
                    findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), "*"), out findData);
                    if (!findHandle.IsInvalid)
                    {
                        do
                        {
                            if (findData.FileName != "." &&
                                findData.FileName != "..")
                            {
                                if (findData.FileAttributes == FileAttributes.Directory)
                                {
                                    folders.Enqueue(LongPath.Combine(currentPath, findData.FileName));
                                }
                            }
                        }while (NativeMethods.FindNextFileW(findHandle, out findData));

                        // Get last Win32 error right after native calls.
                        // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error.
                        errorCode = Marshal.GetLastWin32Error();
                        if (findHandle != null &&
                            !findHandle.IsInvalid)
                        {
                            findHandle.Dispose();
                        }
                        NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode,
                                                                              new int[] {
                            NativeMethods.ERROR_SUCCESS,
                            NativeMethods.ERROR_NO_MORE_FILES,
                            NativeMethods.ERROR_FILE_NOT_FOUND
                        });
                    }
                    else
                    {
                        NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] {
                            NativeMethods.ERROR_SUCCESS,
                            NativeMethods.ERROR_NO_MORE_FILES,
                            NativeMethods.ERROR_FILE_NOT_FOUND
                        });
                    }
                }

                findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), searchPattern), out findData);
                if (!findHandle.IsInvalid)
                {
                    do
                    {
                        if (findData.FileName != "." &&
                            findData.FileName != "..")
                        {
                            if ((filter == FilesOrDirectory.All) ||
                                (filter == FilesOrDirectory.Directory && findData.FileAttributes == FileAttributes.Directory) ||
                                (filter == FilesOrDirectory.File && findData.FileAttributes != FileAttributes.Directory))
                            {
                                yield return(LongPath.Combine(currentPath, findData.FileName));
                            }
                        }
                    }while (NativeMethods.FindNextFileW(findHandle, out findData));

                    // Get last Win32 error right after native calls.
                    // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error.
                    errorCode = Marshal.GetLastWin32Error();
                    if (findHandle != null &&
                        !findHandle.IsInvalid)
                    {
                        findHandle.Dispose();
                    }
                    NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode,
                                                                          new int[] {
                        NativeMethods.ERROR_SUCCESS,
                        NativeMethods.ERROR_NO_MORE_FILES,
                        NativeMethods.ERROR_FILE_NOT_FOUND
                    });
                }
                else
                {
                    NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] {
                        NativeMethods.ERROR_SUCCESS,
                        NativeMethods.ERROR_NO_MORE_FILES,
                        NativeMethods.ERROR_FILE_NOT_FOUND
                    });
                }
            }
#endif
        }