我正在尝试编写一些代码来监视本地工作站上的 TFS 工作区,但目前我在触发事件时遇到了问题。
例如,如果我在我的工作空间中映射一个新文件夹,我想订阅 versionControl.UpdatedWorkspace 事件,如果我执行“get”,我想映射到 versionControl.Getting 事件。下面的代码是一个我认为应该可以工作的控制台应用程序,但是当我这样做时,什么也没有发生。有谁知道如何成功订阅这些事件?
VS2010、TFS 2010、WinXP SP3
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace TestEventHanling
{
class Program
{
static void Main(string[] args)
{
Uri serverUri = new Uri(@"http://TfsServer:8080/tfs/collection");
using (TfsTeamProjectCollection collection = new TfsTeamProjectCollection(serverUri, CredentialCache.DefaultCredentials))
{
VersionControlServer versionControl = (VersionControlServer)collection.GetService(typeof(VersionControlServer));
versionControl.UpdatedWorkspace += new WorkspaceEventHandler(OnUpdatedWorkspace);
versionControl.Getting += new GettingEventHandler(OnGetting);
Console.WriteLine("Press \'q\' to quit.");
while (Console.Read() != 'q') ;
}
}
internal static void OnUpdatedWorkspace(object sender, WorkspaceEventArgs e)
{
foreach (WorkingFolder wf in e.Workspace.Folders)
{
Console.WriteLine("Workspace updated {0}", wf.ServerItem);
}
}
internal static void OnGetting(Object sender, GettingEventArgs e)
{
Console.WriteLine("Getting: {0}, status: {1}", e.TargetLocalItem, e.Status);
}
}
}