如何使用Team Foundation Server 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()方法获取此项目的所有项目,然后删除工作空间。 这是正确的方法吗? 任何例子都会有所帮助。

我最终使用了一种似乎Item.DownloadFile()的不同方法,主要是利用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; } } 

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

对于在引用中工作的项目,应添加Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.VersionControl.Client引用,并在代码中using Microsoft.TeamFoundation.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; } } } 

你的方法是有效的。

您的错误在您的项目路径中。 使用这样的东西:

 string projectPath = "$/PathToApp/TestApp"; 

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

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

我有类似的情况,我需要从tfs下载’a’文件夹的内容到现有的工作区,而不创建新的工作区。 在上述答案的帮助下,我能够将一些对我来说很好的东西放在一起。 但是有一个限制。 这适用于’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://:8080/tfs/DefaultCollection"; // Get the version control server TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl)); VersionControlServer vcs = teamProjectCollection.GetService(); String Sourcepath = args[0]; // The folder path in TFS - "$///" 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 >> DownloadFolder.exe "$///" "C:\MyTempFolder"