/// <summary> /// Creates the symbolic link. /// </summary> /// <param name="linkPath">Link path.</param> /// <param name="targetPath">Target path.</param> /// <param name="linkFlag">Type of the symbolic link.</param> /// <exception cref="ArgumentNullException"><paramref name="targetPath"/> or <paramref name="linkPath"/> is <see langword="null"/> or empty.</exception> /// <exception cref="ArgumentException"><paramref name="targetPath"/> is equal to the <paramref name="linkPath"/>.</exception> /// <exception cref="FileNotFoundException"><paramref name="targetPath"/> was not found and <paramref name="linkFlag"/> is <see cref="SymbolicLinkFlag.File"/>.</exception> /// <exception cref="DirectoryNotFoundException"><paramref name="targetPath"/> was not found and <paramref name="linkFlag"/> is <see cref="SymbolicLinkFlag.Directory"/>.</exception> /// <exception cref="InvalidOperationException">Symbolic link was not created.</exception> /// <remarks> /// The <c>SE_CREATE_SYMBOLIC_LINK_NAME</c> privilege is required to create symbolic links. /// </remarks> private static void CreateSymbolicLink(string linkPath, string targetPath, SymbolicLinkFlag linkFlag) { if (string.IsNullOrEmpty(targetPath)) { throw new ArgumentNullException(nameof(targetPath)); } if (string.IsNullOrEmpty(linkPath)) { throw new ArgumentNullException(nameof(linkPath)); } if (string.Equals(targetPath, linkPath, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Target path is equal to the link path: {0}", targetPath)); } string normalizedLinkPath = LongPathCommon.NormalizePath(linkPath); string normalizedTargetPath = LongPathCommon.NormalizePath(targetPath); if (!LongPathCommon.Exists(targetPath)) { if (linkFlag == SymbolicLinkFlag.Directory) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Target directory not found: {0}", targetPath)); } throw new FileNotFoundException("Target file not found.", targetPath); } if (!NativeMethods.CreateSymbolicLink(normalizedLinkPath, normalizedTargetPath, linkFlag)) { Exception nativeException = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to create symbolic link: {0} -> {1}", linkPath, targetPath), nativeException); } }
internal static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLinkFlag dwFlags);
public static extern Boolean8 CreateSymbolicLinkW( string lpSymlinkFileName, string lpTargetFileName, SymbolicLinkFlag dwFlags);
/// <summary> /// Creates a create symbolic link operation /// </summary> /// <remarks> /// This method is for testing symlinks whose target either /// (a) cannot be represented using FileArtifact/AbsolutePath (e.g., path contains an illegal char), or /// (b) should not be represented using these structs (e.g., target's path is a relative path) /// </remarks> public static Operation CreateSymlink(FileOrDirectoryArtifact linkPath, string target, SymbolicLinkFlag symLinkFlag, bool doNotInfer = false) { return(new Operation(Type.CreateSymlink, linkPath: linkPath, additionalArgs: target, symLinkFlag: symLinkFlag, doNotInfer: doNotInfer)); }
private static extern bool ExternWinCreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLinkFlag dwFlags);
public static extern bool CreateSymbolicLink(string symlinkFileName, string targetFileName, SymbolicLinkFlag flags);
public static extern bool CreateSymbolicLink( /* [in] */ string linkFileName, /* [in] */ string targetFileName, /* [in] */ SymbolicLinkFlag flags);
public static extern bool CreateSymbolicLink( [In] string lpSymlinkFileName, [In] string lpTargetFileName, [In] SymbolicLinkFlag dwFlags);