【问题标题】:Execute tf.exe (TFS) from C# halts从 C# 停止执行 tf.exe (TFS)
【发布时间】:2012-06-11 10:13:57
【问题描述】:

我需要从 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);
}

【问题讨论】:

标签: c# tfs processstartinfo


【解决方案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)

【讨论】:

  • 谢谢,不知道怎么选项目?上面的代码返回整个服务器的最新变更集
  • 抱歉刚刚意识到这根本不是我想要的,我的第一个代码示例使用本地工作区变更集。您的代码使用服务器上的最新版本。我需要本地版本,因为构建服务器正在检查代码并进行构建,如果有人在构建和检查版本之间提交,则版本将是错误的
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 2011-07-27
      相关资源
      最近更新 更多