public static void CreateSymlinkTo(this PathSpec linkPath, PathSpec targetPath)
 {
     switch (targetPath.GetPathType())
     {
         case PathType.File:
             if (!CreateSymbolicLink(linkPath.ToString(), targetPath.ToString(), TargetIsAFile))
             {
                 Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
             }
             break;
         default:
             if (!CreateSymbolicLink(linkPath.ToString(), targetPath.ToString(), TargetIsADirectory) || Marshal.GetLastWin32Error() != 0)
             {
                 try
                 {
                     Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                 }
                 catch (COMException exception)
                 {
                     throw new IOException(exception.Message, exception);
                 }
             }
             break;
     }
 }
 public static void RenameTo(this PathSpec source, PathSpec target)
 {
     switch (source.GetPathType())
     {
         case PathType.File:
             File.Move(source.ToString(), target.ToString());
             break;
         case PathType.Folder:
             Directory.Move(source.ToString(), target.ToString());
             break;
     }
 }
 public int CompareTo(PathSpec other)
 {
     int compareCounts = Components.Count - other.Components.Count;
     if (compareCounts != 0)
         return compareCounts;
     for (int i = 0; i < Components.Count; i++)
     {
         int compareElement = Components[i].CompareTo(other.Components[i]);
         if (compareElement != 0)
             return compareElement;
     }
     return 0;
 }
 internal PathSpec(PathSpec other)
     : this(other.Flags, other.DirectorySeparator, other.Components)
 {
 }
 public bool Equals(PathSpec other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     //if (Flags != other.Flags)
     //    return false;
     if (!string.Equals(DirectorySeparator, other.DirectorySeparator))
         return false;
     StringComparison flags = Flags.ToStringComparison(other.Flags);
     if (Components.Count != other.Components.Count)
         return false;
     for (int i = 0; i < Components.Count; i++)
     {
         if (!string.Equals(Components[i], other.Components[i], flags))
             return false;
     }
     return true;
 }
        /// <summary>
        /// Creates a junction point from the specified directory to the specified target directory.
        /// </summary>
        /// <remarks>
        /// Only works on NTFS.
        /// </remarks>
        /// <param name="junctionPoint">The junction point path</param>
        /// <param name="targetDir">The target directory</param>
        /// <param name="overwrite">If true overwrites an existing reparse point or empty directory</param>
        /// <exception cref="IOException">Thrown when the junction point could not be created or when
        /// an existing directory was found and <paramref name="overwrite" /> if false</exception>
        public static void CreateJunctionPointTo(this PathSpec junctionPoint, PathSpec targetDir, bool overwrite)
        {
            targetDir = targetDir.ToAbsolute();

            if (targetDir.GetPathType() != PathType.Folder)
                throw new IOException("Target path does not exist or is not a directory.");

            if (junctionPoint.GetPathType() == PathType.Folder)
            {
                if (!overwrite)
                    throw new IOException("Directory already exists and overwrite parameter is false.");
            }
            else
            {
                junctionPoint.Create(PathType.Folder);
            }

            using (SafeFileHandle handle = OpenReparsePoint(junctionPoint.ToString(), EFileAccess.GenericWrite))
            {
                byte[] targetDirBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(targetDir.ToString()));

                REPARSE_DATA_BUFFER reparseDataBuffer = new REPARSE_DATA_BUFFER();

                reparseDataBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
                reparseDataBuffer.ReparseDataLength = (ushort)(targetDirBytes.Length + 12);
                reparseDataBuffer.SubstituteNameOffset = 0;
                reparseDataBuffer.SubstituteNameLength = (ushort)targetDirBytes.Length;
                reparseDataBuffer.PrintNameOffset = (ushort)(targetDirBytes.Length + 2);
                reparseDataBuffer.PrintNameLength = 0;
                reparseDataBuffer.PathBuffer = new byte[0x3ff0];
                Array.Copy(targetDirBytes, reparseDataBuffer.PathBuffer, targetDirBytes.Length);

                int inBufferSize = Marshal.SizeOf(reparseDataBuffer);
                IntPtr inBuffer = Marshal.AllocHGlobal(inBufferSize);

                try
                {
                    Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);

                    int bytesReturned;
                    bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_SET_REPARSE_POINT,
                        inBuffer, targetDirBytes.Length + 20, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

                    if (!result)
                        ThrowLastWin32Error("Unable to create junction point.");
                }
                finally
                {
                    Marshal.FreeHGlobal(inBuffer);
                }
            }
        }
 public static bool TryParsePathSpec(string path, out PathSpec pathSpec, out string error, PathFlags flags = PathFlags.UseDefaultsForGivenPath)
 {
     if (flags.HasFlag(PathFlags.UseDefaultsFromUtility) && flags.HasFlag(PathFlags.UseDefaultsForGivenPath))
         throw new ArgumentException("Cannot specify both PathFlags.UseDefaultsFromUtility and PathFlags.UseDefaultsForGivenPath");
     if (flags.HasFlag(PathFlags.UseDefaultsFromUtility))
         flags = GetDefaultFlagsForThisEnvironment();
     error = string.Empty;
     pathSpec = null;
     var isWindowsStyle = path.Contains("\\") || path.Contains(":");
     var isUnixStyle = path.Contains("/");
     if (isWindowsStyle && isUnixStyle)
     {
         error = "Cannot mix slashes and backslashes in the same path";
         return false;
     }
     if (isWindowsStyle)
     {
         if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath))
             flags = PathFlags.None;
         if (path.Length > 1 && path.EndsWith("\\"))
             path = path.Substring(0, path.Length - 1);
         var colonIdx = path.LastIndexOf(':');
         if (colonIdx > -1 && (colonIdx != 1 || !char.IsLetter(path[0]) || (path.Length > 2 && path[2] != '\\')))
         {
             error = "A Windows path may not contain a : character, except as part of the drive specifier.";
             return false;
         }
         var isAbsolute = path.IsAbsoluteWindowsPath();
         if (isAbsolute)
         {
             var components = path.Split('\\').ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "\\", components);
         }
         else if (path.StartsWith("."))
         {
             var components = path.Split('\\').ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "\\", components);
         }
         else if (path.StartsWith("\\\\"))
         {
             var components = "\\\\".ItemConcat(path.Substring(2).Split('\\')).ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "\\", components);
         }
         else if (path.StartsWith("\\"))
         {
             var components = "\\".ItemConcat(path.Substring(1).Split('\\')).ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "\\", components);
         }
         else
         {
             var components = ".".ItemConcat(path.Split('\\')).ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "\\", components);
         }
         return true;
     }
     if (isUnixStyle)
     {
         if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath))
             flags = PathFlags.CaseSensitive;
         if (path.Length > 1 && path.EndsWith("/"))
             path = path.Substring(0, path.Length - 1);
         if (path.Contains(":"))
         {
             error = "A Unix path may not contain a : character.";
             return false;
         }
         var isAbsolute = path.IsAbsoluteUnixPath();
         if (isAbsolute)
         {
             var components = "/".ItemConcat(path.Substring(1).Split('/')).ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "/", components);
         }
         else if (path.StartsWith("."))
         {
             var components = path.Split('/').ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "/", components);
         }
         else
         {
             var components = ".".ItemConcat(path.Split('/')).ToList();
             components.RemoveWhere((i, str) => i != 0 && str == ".");
             if (components.Any(String.IsNullOrEmpty))
             {
                 error = "Must not contain any directories that have empty names";
                 return false;
             }
             if (components.IsAncestorOfRoot())
             {
                 error = "Must not point to an ancestor of the filesystem root";
                 return false;
             }
             pathSpec = new PathSpec(flags, "/", components);
         }
         return true;
     }
     // If we reach this point, there are no backslashes or slashes in the path, meaning that it's a
     // path with one element.
     if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath))
         flags = GetDefaultFlagsForThisEnvironment();
     if (path == ".." || path == ".")
         pathSpec = new PathSpec(flags, DefaultDirectorySeparator, path);
     else
         pathSpec = new PathSpec(flags, DefaultDirectorySeparator, ".", path);
     return true;
 }
 public static bool TryParsePathSpec(string path, out PathSpec pathSpec, PathFlags flags = PathFlags.UseDefaultsForGivenPath)
 {
     string error = string.Empty;
     return TryParsePathSpec(path, out pathSpec, out error, flags);
 }