【问题标题】:Clone File Structure Of Pending Changes In Visual Studio克隆 Visual Studio 中未决更改的文件结构
【发布时间】:2024-04-29 21:25:01
【问题描述】:

我在 Visual Studio 中使用 TFS 和 Git,我希望能够将所有挂起的文件和文件结构导出或克隆到我选择的文件夹中,以便可以在本地备份我的挂起更改而不是搁置或存储待处理的文件。我目前必须在 Windows 资源管理器中打开每个文件,然后运行脚本将文件及其文件结构复制到我的备份位置,这既费时又容易出现手动错误。有谁知道插件、TFS 中的功能、任何 Git 工具或 Visual Studio 中的某些东西,它们会将挂起的文件及其文件结构复制到我选择的文件夹中?

【问题讨论】:

    标签: git visual-studio github tfs


    【解决方案1】:

    您可以使用 TFS API 来实现您想要的。获取所有文件、文件夹并获取它们的目录。判断文件是否正在等待更改。然后使用您的脚本将文件复制到您的备份位置。

    工作区代码供您参考:

    private void PopulateTreeView(string workspaceName)
    {
        // Connect to TFS - VersionControlServer Service
        var tfs =
            TfsTeamProjectCollectionFactory.GetTeamProjectCollection
            (new Uri("https://tfs2010:8080/defaultcollection"));
        var vcs = tfs.GetService<VersionControlServer>();
    
        // Get the workspace the user has currently selected
        var workspace = vcs.QueryWorkspaces(workspaceName, 
            vcs.AuthorizedUser, Environment.MachineName)[0];
        _workspace = workspace;
        tvWksNavigator.Nodes.Clear();
    
        // Loop through all folders and get directories and files
        foreach (var folder in workspace.Folders)
        {
            var info = new DirectoryInfo(folder.LocalItem);
            if (info.Exists)
            {
                var rootNode = new TreeNode(info.Name) { Tag = info };
                GetDirectories(info.GetDirectories(), rootNode);
                tvWksNavigator.Nodes.Add(rootNode);
            }
        }
    }
    

    使用 QueryPendingChanges 方法,可以传递一个文件路径,查看文件是否正在等待更改,如果是,锁定类型是什么,还可以获取文件的下载详细信息。

    var status = _workspace.QueryPendingSets(new[] { new ItemSpec(
                                                        dir.FullName, 
                                                        RecursionType.None) },
                                                        _workspace.Name, vcs.AuthorizedUser, 
                              false);
    

    【讨论】: