// Create a symbolic link at "newpath" that points to "oldpath". // Throws "NotSupportedException" if the filesystem does not // support the creation of symbolic links or ".lnk" files. public static void CreateLink(String oldpath, String newpath) { // Validate and normalize the parameters. Directory.ValidatePath(oldpath); Directory.ValidatePath(newpath); oldpath = Path.NormalizeSeparators(oldpath); newpath = Path.NormalizeSeparators(newpath); // Try to create the symlink using the engine. Errno error = FileMethods.CreateLink(oldpath, newpath); if (error == Errno.Success) { return; } else if (error != Errno.EPERM) { Directory.HandleErrorsFile(error); } // Bail out if we are not on Windows. if (!IsWindows()) { throw new NotSupportedException(_("IO_NotSupp_Symlinks")); } // Make sure that we have ".lnk" on the end of the pathname. if (!EndsInLnk(newpath)) { if (FileMethods.GetFileType(newpath) != FileType.unknown) { // There already exists something at this location. Directory.HandleErrorsFile(Errno.EEXIST); } newpath += ".lnk"; } if (FileMethods.GetFileType(newpath) != FileType.unknown) { // There already exists something at this location. Directory.HandleErrorsFile(Errno.EEXIST); } // Create the shortcut information to place in the file. ShortcutInformation info = new ShortcutInformation(); info.Description = oldpath; info.RelativePath = oldpath; info.ShowWindow = ShowWindow.Normal; // Write the shortcut information to the file. WriteShortcut(newpath, info, false); }
// Determine if a pathname is a symbolic link. public static bool IsSymbolicLink(String pathname) { // Validate the parameter and normalize it. if (pathname == null) { throw new ArgumentNullException("pathname"); } pathname = Path.NormalizeSeparators(pathname); // If we are on Windows, then check for a ".lnk" file. if (IsWindows()) { if (!EndsInLnk(pathname)) { // We need to add ".lnk" to the pathname first. if (FileMethods.GetFileType(pathname) != FileType.unknown) { // The path exists, so it cannot be a symlink. return(false); } pathname += ".lnk"; } if (FileMethods.GetFileType(pathname) != FileType.regularFile) { // The path does not exist or is not a regular file. return(false); } return(true); } // Get the file type from the engine and check it. FileType type = FileMethods.GetFileType(pathname); return(type == FileType.symbolicLink); }
// Read the contents of a symbolic link. Returns null if not a // symbolic link, or throws an exception if the pathname is invalid. public static String ReadLink(String pathname) { // Validate the parameter and normalize it. Directory.ValidatePath(pathname); pathname = Path.NormalizeSeparators(pathname); // Read the contents of the symlink using the engine. String contents; Errno error = FileMethods.ReadLink(pathname, out contents); if (error != Errno.Success) { Directory.HandleErrorsFile(error); } if (contents != null) { return(contents); } // Bail out if we aren't on Windows. if (!IsWindows()) { return(null); } // Make sure that we have ".lnk" on the end of the name. if (!EndsInLnk(pathname)) { if (FileMethods.GetFileType(pathname) != FileType.unknown) { // Something exists here and it does not have a ".lnk" // extension, so it cannot possibly be a symlink. return(null); } pathname += ".lnk"; } FileType type = FileMethods.GetFileType(pathname); if (type == FileType.unknown) { // The pathname does not exist. Directory.HandleErrorsFile(Errno.ENOENT); } else if (type != FileType.regularFile) { // The pathname exists but it is not a regular file, // so it cannot possibly be a symbolic link. return(null); } // Read the contents of the Windows shortcut file. ShortcutInformation info = ReadShortcut(pathname); if (info == null) { return(null); } else { return(info.RelativePath); } }