从工作区仅下载所需的解决方案

时间:2018-03-01 14:19:11

标签: c# tfs version-control

我有一段代码可以设置工作空间并以编程方式从tfs工作空间下载文件。我想扩展以指定我可以在该工作空间中下载哪些解决方案。我有什么想法可以做到这一点吗?

private GetStatus DownloadLatestFiles()
    {
        Workspace workspace = null;

        try
        {
            workspace = SetupWorkSpace();

            workspace.Map(_repositoryCredentials.RepositoryProjectPath, _repositoryCredentials.WorkingDirectory);
            GetRequest request = new GetRequest(new ItemSpec(_repositoryCredentials.RepositoryProjectPath, RecursionType.Full), VersionSpec.Latest);

            return workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite);
        }
        catch
        {
            throw;
        }
    }

    private Workspace SetupWorkSpace()
    {
        VersionControlServer sourceControl = SetupVersionControlRepositoryConnection();
        Workspace workspace = sourceControl.QueryWorkspaces(
                 Environment.MachineName,
                 sourceControl.AuthorizedUser,
                 Environment.MachineName).SingleOrDefault();

        if (workspace == null)
        {
            workspace = sourceControl.CreateWorkspace(Environment.MachineName, sourceControl.AuthenticatedUser, "newworkspace");
        }

        return workspace;
    }

我做了一个改变所以它现在显示......

private GetStatus DownloadLatestFiles()
    {
        Workspace workspace = null;
        GetStatus status = null;

        try
        {
            workspace = SetupWorkSpace();
            List<Solution> services = _serviceList.GetAll();

           foreach (Solution solution in services)
            {
                WorkingFolder workingFolder = new WorkingFolder(ConvertLocalToTfsPath(solution), GetSolutionFolder(solution));
                workspace.CreateMapping(workingFolder);

                //GetRequest request = new GetRequest(new ItemSpec(_repositoryCredentials.RepositoryProjectPath, RecursionType.Full), VersionSpec.Latest);
                //status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite);
                status = workspace.Get();
            }
        }
        catch
        {
            throw;
        }

        return status;
    }

目前仍在下载所有文件。

2 个答案:

答案 0 :(得分:1)

只需添加以下代码:

     var workspace = sourceControl .CreateWorkspace("workspaceName","workspaceOwner");

        String ServerFolder = @"$/TeamProject/Solution1";
        String LocalFolder = @"D:\Folder\";

        WorkingFolder workfolder = new WorkingFolder(ServerFolder,LocalFolder);
        workspace.CreateMapping(workfolder);

        workspace.Get();

请参阅此文章了解详情:Download Project from TFS online With .NET C#

答案 1 :(得分:0)

我找到了答案。只需稍微改编一下上面的答案。

private List<GetStatus> DownloadLatestFiles()
    {
        Workspace workspace = null;
        List<GetStatus> statusResult = new List<GetStatus>();

        try
        {
            workspace = SetupWorkSpace();
            List<Solution> services = _serviceList.GetAll();

           foreach (Solution solution in services)
            {
                WorkingFolder workingFolder = new WorkingFolder(ConvertLocalToTfsPath(solution), GetSolutionFolder(solution));
                workspace.CreateMapping(workingFolder);

                GetRequest request = new GetRequest(new ItemSpec(ConvertLocalToTfsPath(solution), RecursionType.Full), VersionSpec.Latest);
                statusResult.Add(workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite));
            }
        }
        catch
        {
            throw;
        }

        return statusResult;
    }