15

我正在尝试使用 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()方法抓取该项目的所有项目,然后删除该工作区。这是正确的方法吗?任何例子都会有所帮助。

4

5 回答 5

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;
    }
}
于 2009-12-09T18:05:14.367 回答
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;
            }
        }
    }
于 2011-07-22T07:32:46.230 回答
5

你的方法是有效的。

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

string projectPath = "$/PathToApp/TestApp";
于 2009-12-02T03:16:14.633 回答
1

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

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

于 2009-12-02T21:20:53.130 回答
0

我有类似的情况,我需要将 'a' 文件夹的内容从 tfs 下载到现有工作区,而不创建新工作区。在上述答案的帮助下,到目前为止,我能够将一些对我来说很好的东西放在一起。但是有一个限制。这适用于仅包含文件而不是其中的另一个文件夹的“a”文件夹的内容-我还没有尝试过。也许这将涉及一些小的更新。共享代码,以防万一有人在搜索这个。我真的很喜欢这种方法不处理工作区 [ -create and 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>>DownloadFolder.exe "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" "C:\MyTempFolder"

于 2016-09-20T20:50:03.000 回答