2

我需要从 TFS 获取修订号,如果我从进程运行 tf.exe,进程会停止。如果我从命令提示符运行相同的命令,它可以工作吗?

int revision;

var repo = "path to repo"

var psi = new ProcessStartInfo("cmd", @"/c ""C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"" properties $/MYProject -recursive /version:W")
{
    UseShellExecute = false,
    ErrorDialog = false,
    CreateNoWindow = false,
    WorkingDirectory = repo,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

using (var p = Process.Start(psi))
{
    p.WaitForExit();
    if (p.ExitCode != 0)
    {
        using (var standardError = p.StandardError)
        {
            Console.WriteLine(standardError.ReadToEnd());
        }
    } 
    else
    {
        using (var standardOutput = p.StandardOutput)
        {
            revision = int.Parse(standardOutput.ReadToEnd());
        }
    }
}

编辑:

我做了这个,工作,我应该去吗?

public int GetLatestChangeSet(string url, string project)
{
    var server = new TeamFoundationServer(new Uri(url));
    var version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

    var items = version.GetItems(string.Format(@"$\{0}", project), RecursionType.Full);
    return items.Items.Max(i => i.ChangesetId);
}
4

3 回答 3

1

您最好使用以下名称空间,其中包含实现该目标所需的所有内容

Microsoft.TeamFoundation.VersionControl.Client 

//this is just an example 

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://myserver:8080/"));
VersionControlServer sourceControl = tpc.GetService<VersionControlServer>();
return sourceControl.GetLatestChangesetId();

http://msdn.microsoft.com/en-us/library/ms228232(v=vs.80 )

于 2012-06-11T10:22:15.393 回答
1

发生错误是因为您的StandardOutput流缓冲区已满并因此阻塞。要阅读标准输入/输出,建议订阅该OutputDataReceived事件。或者,启动另一个线程以不断地从StandardOutput流中读取数据。

有关完整的代码示例,请参阅 OutputDataReceived 事件文档中的示例

更好的解决方案是使用 Massimiliano Peluso 建议的 TFS API。但这是您的方法失败的原因。

于 2012-06-11T13:16:39.520 回答
1

我最终得到了这个使用本地工作区修订的解决方案

public class ReadTfsRevisionTask : Task
{
    public override bool Execute()
    {
        try
        {
            ChangesetId = GetLatestChangeSet(Server, Project);
            return true;
        }
        catch
        {
            return false;
        }
    }

    private int GetLatestChangeSet(string url, string project)
    {
        project = string.Format(@"$/{0}", project);

        var server = new TeamFoundationServer(new Uri(url));
        var version = server.GetService<VersionControlServer>();

        var workspace = version.QueryWorkspaces(null, WindowsIdentity.GetCurrent().Name, System.Environment.MachineName).First();
        var folder = workspace.Folders.First(f => f.ServerItem == project);

        return workspace.GetLocalVersions(new[] { new ItemSpec(folder.LocalItem, RecursionType.Full) }, false)
            .SelectMany(lv => lv.Select(l => l.Version)).Max();
    }

    [Required]
    public string Server { get; set; }

    [Required]
    public string Project { get; set; }

    [Output]
    public int ChangesetId { get; set; }

}
于 2012-06-11T13:30:57.927 回答