c# - 获取 TFS 中工作区之间的最新信息和差异

标签 c# tfs getlatest

我想从 TFS 获取最新更改以及本地工作区和服务器版本之间的差异,因为我使用了从 here 获得的代码

     private static void GetLatest(string username, string password, string path_to_download,
              string tf_src_path)
    {

        Uri collectionUri = new Uri(PathConstants.uri);

        NetworkCredential credential = new NetworkCredential(username, password);
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(PathConstants.uri), credential);
        tfs.EnsureAuthenticated();
        VersionControlServer vc = tfs.GetService<VersionControlServer>();
        foreach (var item in vc.GetItems(PathConstants.tfsRoot + tf_src_path, VersionSpec.Latest, RecursionType.Full).Items)
        {
            string relativePath = _BuildRelativePath(path_to_download, item.ServerItem);

            switch (item.ItemType)
            {
                case ItemType.Any:
                    throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder.");
                case ItemType.File:
                    item.DownloadFile(relativePath);
                    break;
                case ItemType.Folder:
                    Directory.CreateDirectory(relativePath);
                    break;
            }
        }
    }

但此代码从源下载所有文件并替换本地工作区中的现有文件。

有没有办法只下载本地和服务器版本的区别?例如如果我删除本地的任何文件/文件夹,它们也应该与与变更集关联的新文件一起下载,而不替换其他文件

最佳答案

这应该会更新所有本地工作区中的所有文件。

private static void GetLatest(string username, string password, string path_to_download,
              string tf_src_path)
    {

        Uri collectionUri = new Uri(PathConstants.uri);
        NetworkCredential credential = new NetworkCredential(username, password);
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(PathConstants.uri), credential);
        tfs.EnsureAuthenticated();
        VersionControlServer vc = tfs.GetService<VersionControlServer>();

        foreach (Workspace workspace in vc.QueryWorkspaces(null, null, System.Environment.MachineName))
            {
                foreach (WorkingFolder folder in workspace.Folders)
                {
                ItemSpec itemSpec = new ItemSpec(folder.ServerItem,  RecursionType.Full);
                ItemSpec[] specs = new ItemSpec[] { itemSpec };
                ExtendedItem[][] extendedItems = workspace.GetExtendedItems(specs, DeletedState.NonDeleted, ItemType.File);
                ExtendedItem[] extendedItem = extendedItems[0];
                    foreach (var item in extendedItem)
                    {
                        if (item.VersionLocal != item.VersionLatest)
                        {
                            vc.DownloadFile(item.SourceServerItem, item.LocalItem);
                        }
                    }
                }
            }
        }

关于c# - 获取 TFS 中工作区之间的最新信息和差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328683/

相关文章:

c# - 如何在不等待 connection.open() 30 秒的情况下快速检查 SQL 连接;

tfs - TFS 中的默认审阅者

SQL-如何让sql自动设置下一个id

Mysql获取组的最后一个元素

c# - 日期时间查询错误

c# - 如何使用 AutomationId 识别 WinForms DevExpress 控件?

powershell - 从 PowerShell 写入 TFS 构建服务器上的测试结果

tfs - TFS/VS 2010 中的标签误解

c# - 输入 public int a 后报错