Execute tf.exe (TFS) from C# halts

I need to get the revision number from TFS, if i run tf.exe from a Process the process halts. If i run the same command from command promts it works?

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

edit:

I did this, works, should I go with it?

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

Answers


you better use the below namespace which contains all you need to achieve that

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)


The error occurs because your StandardOutput stream buffer is full and thus blocks. To read standard input/output it's recommended to subscribe to the OutputDataReceived event. Alternatively, spin up another thread to constantly read the data from the StandardOutput stream.

See the example on the OutputDataReceived event docs for a complete code sample.

A better solution would be to use the TFS API as suggested by Massimiliano Peluso. But this is the reason for your approach failing.


I ended up with this solution which uses the local workspace revision

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

}

Need Your Help

Bootstrap navbar dropdown displaying menu-items horizontally

javascript jquery html css twitter-bootstrap

I've been working on a new project where a navbar required to be center of the page at top. I have the following html...

Overlapping Intervals

algorithm intervals

Assume that you are given a set of intervals (not necessarily integral in length). How do you determine if there is an overlap between any two intervals in the given set? I am wondering if there is a