从C#halts执行tf.exe(TFS)

时间:2012-06-11 10:13:58

标签: c# tfs processstartinfo

我需要从TFS获取修订号,如果我从进程运行tf.exe,则进程暂停。如果我从命令promts运行相同的命令它有效吗?

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);
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

请参阅the example on the OutputDataReceived event docs for a complete code sample

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

答案 2 :(得分: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; }

}