Exemplo n.º 1
0
        public void RenameItem(WorkspaceItem item)
        {
            var path        = item.Path;
            var name        = Path.GetFileNameWithoutExtension(path);
            var nameWithExt = Path.GetFileName(path);
            var ext         = Path.GetExtension(path);

            var result = UIServices.CreateDialog <PromptData>(UI.Enums.DialogType.GenericPrompt, "Rename", name);

            if (!result.Success)
            {
                return;
            }

            if (name.Equals(result.Data.Value, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            switch (item.Type)
            {
            case WorkspaceItemType.Solution:
                var newSlnPath = Path.Combine(Path.GetDirectoryName(path), result.Data.Value + ext);
                File.Move(path, newSlnPath);
                Instance.OpenSolution(newSlnPath);
                break;

            case WorkspaceItemType.Project:
                var newPrjPath = Path.Combine(Path.GetDirectoryName(path), result.Data.Value + ext);
                File.Move(path, newPrjPath);

                foreach (var proj in Workspace.SlnFile.Projects)
                {
                    if (Path.GetFileName(proj.FilePath).Equals(nameWithExt, StringComparison.OrdinalIgnoreCase))
                    {
                        proj.Name     = result.Data.Value;
                        proj.FilePath = Path.Combine(Path.GetDirectoryName(proj.FilePath), result.Data.Value + ext);
                    }
                }

                Workspace.SlnFile.Write();
                Instance.OpenSolution(Workspace.SlnFile.FullPath);
                break;

            case WorkspaceItemType.Folder:
                var newDirPath = Path.Combine(Path.GetDirectoryName(path), result.Data.Value);
                Directory.Move(path, newDirPath);
                Instance.UpdateWorkspaceItems();
                break;

            case WorkspaceItemType.File:
                var newFilePath = Path.Combine(Path.GetDirectoryName(path), result.Data.Value + ext);
                File.Move(path, newFilePath);
                Instance.UpdateWorkspaceItems();
                break;

            default:
                break;
            }
        }