Exemplo n.º 1
0
        public static void Create(string target, string symbolicLink)
        {
            if (!File.Exists(target) && !Directory.Exists(target))
            {
                FileNotFoundException ex = new FileNotFoundException("Link target does not exist", target);

                throw ex;
            }
            switch (Environment.OSVersion.Platform)
            {
            default:
                throw new InvalidOperationException("Unsupported operating system");

            case PlatformID.Unix:
            case PlatformID.MacOSX:
                if (Unix.symlink(target, symbolicLink) < 0)
                {
                    throw Unix.Failure("Could not create link");
                }
                break;

            case PlatformID.Win32NT:
                Win32.SymbolicLinkFlags flags = File.Exists(target)
                        ? Win32.SymbolicLinkFlags.File
                        : Win32.SymbolicLinkFlags.Directory;

                flags |= Win32.SymbolicLinkFlags.AllowUnprivilegedCreate;

                target = !Path.IsPathRooted(target)
                        ? target
                        : "\\\\?\\" + Path.GetFullPath(target);

                symbolicLink = "\\\\?\\" + Path.GetFullPath(symbolicLink);
                if (!Win32.CreateSymbolicLink(symbolicLink, target, flags))
                {
                    throw Win32.Failure("Could not create link");
                }
                break;
            }
        }