示例#1
0
        /// <summary>
        ///     Enables/disables the directory redirection.
        /// </summary>
        /// <param name="option">
        ///     The option that determines whether the redirect will be enabled or disabled.
        /// </param>
        /// <param name="dirMap">
        ///     The diretory map.
        ///     <para>
        ///         The key is used for the source directory, and the value is used for the
        ///         destination directory.
        ///     </para>
        /// </param>
        public static void DirRedirection(PortalizerActions option, Dictionary <string, string> dirMap)
        {
            if (dirMap?.Any() != true)
            {
                return;
            }
            var hash  = nameof(DirRedirection).Encrypt();
            var hPath = PathEx.Combine(Attributes.TempDir, hash);

            switch (option)
            {
            case PortalizerActions.Disable:
                FileEx.TryDelete(hPath);
                break;

            default:
                if (File.Exists(hPath))
                {
                    DirRedirection(PortalizerActions.Disable, dirMap);
                }
                FileEx.Create(hPath);
                break;
            }
            foreach (var data in dirMap)
            {
                if (string.IsNullOrWhiteSpace(data.Key) || string.IsNullOrWhiteSpace(data.Value))
                {
                    continue;
                }
                var srcPath  = PathEx.Combine(data.Key);
                var destPath = PathEx.Combine(data.Value);
                DirectoryEx.SetAttributes(srcPath, FileAttributes.Normal);
                var backupPath = $"{srcPath}-{{{EnvironmentEx.MachineId}}}.backup";
                switch (option)
                {
                case PortalizerActions.Disable:
                    DirectoryEx.SetAttributes(backupPath, FileAttributes.Normal);
                    if (DirectoryEx.DestroySymbolicLink(srcPath, true))
                    {
                        continue;
                    }
                    try
                    {
                        if (Directory.Exists(srcPath))
                        {
                            DirectoryEx.SetAttributes(srcPath, FileAttributes.Normal);
                            DirectoryEx.Copy(srcPath, destPath, true, true);
                            Directory.Delete(srcPath, true);
                        }
                        if (Directory.Exists(backupPath))
                        {
                            DirectoryEx.Copy(backupPath, srcPath, true, true);
                            Directory.Delete(backupPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                    }
                    break;

                default:
                    if (DirectoryEx.CreateSymbolicLink(srcPath, destPath, true))
                    {
                        DirectoryEx.SetAttributes(backupPath, FileAttributes.Normal);
                        continue;
                    }
                    try
                    {
                        if (Directory.Exists(srcPath))
                        {
                            if (!Directory.Exists(backupPath))
                            {
                                DirectoryEx.Move(srcPath, backupPath);
                                DirectoryEx.SetAttributes(backupPath, FileAttributes.Hidden);
                            }
                            if (Directory.Exists(srcPath))
                            {
                                Directory.Delete(srcPath, true);
                            }
                        }
                        if (Directory.Exists(destPath))
                        {
                            DirectoryEx.SetAttributes(destPath, FileAttributes.Normal);
                            DirectoryEx.Copy(destPath, srcPath);
                        }
                        if (!Directory.Exists(srcPath))
                        {
                            Directory.CreateDirectory(srcPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                    }
                    break;
                }
            }
        }