如何使用Team Foundation Server SDK获取最新版本的源代码?

时间:2009-12-01 17:22:39

标签: c# tfs tfs-sdk

我正在尝试使用SDK以编程方式从TFS中提取最新版本的源代码,而且我所做的某些方式不起作用:

string workspaceName = "MyWorkspace";
string projectPath = "/TestApp";
string workingDirectory = "C:\Projects\Test\TestApp";

VersionControlServer sourceControl; // actually instantiated before this method...

Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
if (workspaces.Length > 0)
{
    sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
}
Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
try
{
    workspace.Map(projectPath, workingDirectory);
    GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
    GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
}
finally
{
    if (workspace != null)
    {
        workspace.Delete();
    }
}

该方法基本上是创建临时工作空间,使用Get()方法获取此项目的所有项目,然后删除工作空间。这是正确的方法吗?任何例子都会有所帮助。

5 个答案:

答案 0 :(得分:11)

我最终使用了一种似乎有用的不同方法,主要是利用Item.DownloadFile()方法:

VersionControlServer sourceControl; // actually instantiated...

ItemSet items = sourceControl.GetItems(sourcePath, VersionSpec.Latest, RecursionType.Full);

foreach (Item item in items.Items)
{
    // build relative path
    string relativePath = BuildRelativePath(sourcePath, item.ServerItem);

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

答案 1 :(得分:7)

我完成并将代码实现为一个按钮作为web asp.net解决方案。

要在参考中使用的项目,应添加Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.VersionControl.Client引用,并在代码中添加语句using Microsoft.TeamFoundation.Client;using Microsoft.TeamFoundation.VersionControl.Client;

    protected void Button1_Click(object sender, EventArgs e)
    {
        string workspaceName = "MyWorkspace";
        string projectPath = @"$/TeamProject"; // the container Project (like a tabel in sql/ or like a folder) containing the projects sources in a collection (like a database in sql/ or also like a folder) from TFS

        string workingDirectory = @"D:\New1";  // local folder where to save projects sources

        TeamFoundationServer tfs = new TeamFoundationServer("http://test-server:8080/tfs/CollectionName", System.Net.CredentialCache.DefaultCredentials);
                                                            // tfs server url including the  Collection Name --  CollectionName as the existing name of the collection from the tfs server 
        tfs.EnsureAuthenticated(); 

        VersionControlServer sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

        Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
        if (workspaces.Length > 0)
        {
            sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
        }
        Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
        try
        {
            workspace.Map(projectPath, workingDirectory);
            GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
            GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
        }
        finally
        {
            if (workspace != null)
            {
                workspace.Delete();
                Label1.Text = "The Projects have been brought into the Folder  " + workingDirectory;
            }
        }
    }

答案 2 :(得分:5)

您的方法有效。

您的错误位于项目路径中。请改用这样的东西:

string projectPath = "$/PathToApp/TestApp";

答案 3 :(得分:1)

我同意Joerage的说法,你的服务器路径可能是罪魁祸首。要更深入地了解正在发生的事情,您需要在VersionControlServer对象上连接一些事件。至少你会想要Getting,NonFatalError和Conflict。

完整列表:http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver_events(VS.80).aspx

答案 4 :(得分:0)

我遇到过类似的情况,我需要将'a'文件夹的内容从tfs下载到现有的工作区,而不需要创建新的工作区。在上述答案的帮助下,我能够将一些对我来说很好的东西放在一起。但是有一个限制。这适用于'a'文件夹的内容只包含文件,而不是其中的另一个文件夹 - 我没有尝试过。也许这会涉及一些小的更新。共享代码,以防万一有人正在搜索此内容。我真的很喜欢这种方法不处理工作区[ -create和delete ]的事实,因为这是不可取的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.IO;

namespace DownloadFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            string teamProjectCollectionUrl = "http://<YourTFSUrl>:8080/tfs/DefaultCollection";  // Get the version control server
            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
            VersionControlServer vcs = teamProjectCollection.GetService<VersionControlServer>();
            String Sourcepath = args[0]; // The folder path in TFS - "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>"
            String DestinationPath = args[1]; //The folder in local machine - "C:\MyTempFolder"
            ItemSet items = vcs.GetItems(Sourcepath, VersionSpec.Latest, RecursionType.Full);
            String FolderName = null;
            foreach (Item item in items.Items)
            {
                String ItemName = Path.GetFileName(item.ServerItem);
                switch (item.ItemType)
                {
                    case ItemType.File:                        
                        item.DownloadFile(Path.Combine(DestinationPath, FolderName, ItemName));
                        break;
                    case ItemType.Folder:
                        FolderName = Path.GetFileName(item.ServerItem);
                        Directory.CreateDirectory(Path.Combine(DestinationPath, ItemName));
                        break;
                }
            }
        }
    }
}

从命令提示符运行时,将所有支持的dll与exe一起复制 CMD&GT;&GT; DownloadFolder.exe "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" "C:\MyTempFolder"