Пример #1
0
        // 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);
        }