/// <summary> /// Changes an existing link target to a new path. /// </summary> /// <param name="linkPath"> The link path. </param> /// <param name="newActualPath"> The new actual path. </param> /// <remarks> /// </remarks> public void ChangeLinkTarget(string linkPath, string newActualPath) { linkPath = linkPath.GetFullPath(); newActualPath = GetActualPath(newActualPath.GetFullPath()); var oldActualPath = GetActualPath(linkPath); if (oldActualPath.Equals(newActualPath, StringComparison.CurrentCultureIgnoreCase)) { return; } if (!IsSymlink(linkPath)) { throw new PathIsNotSymlinkException(linkPath); } if (Directory.Exists(linkPath)) { ReparsePoint.ChangeReparsePointTarget(linkPath, newActualPath); } else if (File.Exists(linkPath)) { DeleteSymlink(linkPath); MakeFileLink(linkPath, newActualPath); } else { throw new DirectoryNotFoundException("Target directory not found"); } }
public void MakeFileLink(string linkPath, string actualFilePath) { linkPath = linkPath.GetFullPath(); actualFilePath = GetActualPath(actualFilePath.GetFullPath()); #if FALSE if (!File.Exists(actualFilePath)) { throw new FileNotFoundException("Cannot link to non-existent file", actualFilePath); } #endif if (File.Exists(linkPath) && IsSymlink(linkPath)) { ReparsePoint.ChangeReparsePointTarget(linkPath, actualFilePath); return; } if (File.Exists(linkPath) || Directory.Exists(linkPath)) { throw new ConflictingFileOrFolderException(linkPath); } Kernel32.CreateSymbolicLink(linkPath, actualFilePath, 0); }
public bool IsSymlink(string linkPath) { if (!ReparsePoint.IsReparsePoint(linkPath)) { return(false); } var reparsePoint = ReparsePoint.Open(linkPath); return(reparsePoint.IsSymlinkOrJunction); }
public void ChangeLinkTarget(string linkPath, string newActualPath) { linkPath = linkPath.GetFullPath(); newActualPath = GetActualPath(newActualPath.GetFullPath()); if (!IsSymlink(linkPath)) { throw new PathIsNotSymlinkException(linkPath); } ReparsePoint.ChangeReparsePointTarget(linkPath, newActualPath); }
/// <summary> /// Gets the actual path. /// </summary> /// <param name="linkPath"> The link path. </param> /// <returns> </returns> /// <remarks> /// </remarks> public string GetActualPath(string linkPath) { linkPath = linkPath.GetFullPath(); if (File.Exists(linkPath)) { string result; GetAlternateStreamData(linkPath, out result); return(result); } if (Directory.Exists(linkPath)) { return(ReparsePoint.GetActualPath(linkPath)); } return(linkPath); }
/// <summary> /// Determines whether the specified link path is symlink. /// </summary> /// <param name="linkPath"> The link path. </param> /// <returns> /// <c>true</c> if the specified link path is symlink; otherwise, <c>false</c> . /// </returns> /// <remarks> /// </remarks> public bool IsSymlink(string linkPath) { linkPath = linkPath.GetFullPath(); if (File.Exists(linkPath)) { if (GetFileInfo(linkPath).NumberOfLinks > 1) { string canonicalFilePath; var alternates = GetAlternateStreamData(linkPath, out canonicalFilePath); var result = !linkPath.Equals(canonicalFilePath, StringComparison.CurrentCultureIgnoreCase); try { if (result && !alternates.Contains(linkPath)) { AddSymlinkToAlternateStream(canonicalFilePath, linkPath); } } catch { // just trying to clean-as-we-go } return(result); } try { var s = new FileInfo(linkPath).GetAlternateDataStream(legacySymlinkInfo); if (s.Exists) { s.Delete(); } } catch { // just try to clean as we go... } return(false); } if (Directory.Exists(linkPath)) { return(ReparsePoint.IsReparsePoint(linkPath)); } throw new FileNotFoundException("Link does not exist"); }
/// <summary> /// Creates a symlink for a directory. If it already exists, it is updated. /// </summary> /// <param name="linkPath"> The link path. </param> /// <param name="actualFolderPath"> The actual folder path. </param> /// <remarks> /// </remarks> public void MakeDirectoryLink(string linkPath, string actualFolderPath) { linkPath = linkPath.GetFullPath(); actualFolderPath = GetActualPath(actualFolderPath.GetFullPath()); if (!Directory.Exists(actualFolderPath)) { throw new FileNotFoundException("Cannot link to non-existent directory", actualFolderPath); } if (Directory.Exists(linkPath) && IsSymlink(linkPath)) { ReparsePoint.ChangeReparsePointTarget(linkPath, actualFolderPath); return; } if (File.Exists(linkPath) || Directory.Exists(linkPath)) { throw new ConflictingFileOrFolderException(linkPath); } ReparsePoint.CreateJunction(linkPath, actualFolderPath); }
public string GetActualPath(string linkPath) { linkPath = linkPath.GetFullPath(); return(ReparsePoint.GetActualPath(linkPath)); }