从 C# 停止执行 tf.exe (TFS)
本文关键字:exe TFS tf 执行 | 更新日期: 2023-09-27 18:36:43
我需要从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);
}
你最好使用以下命名空间,其中包含实现它所需的一切
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)
发生此错误的原因是您的StandardOutput
流缓冲区已满,因此阻塞。若要读取标准输入/输出,建议订阅 OutputDataReceived
事件。或者,启动另一个线程以不断从StandardOutput
流中读取数据。
有关完整的代码示例,请参阅 OutputDataReceived 事件文档中的示例。
更好的解决方案是使用 Massimiliano Peluso 建议的 TFS API。但这就是你的方法失败的原因。
我最终得到了这个使用本地工作区修订版的解决方案
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; }
}