Listen the Getting event of Version Control

This behavior can be done in many ways, Hongye supplied a solution in http://social.msdn.microsoft.com/Forums/en-US/tfsgeneral/thread/c6a4dc3c-39f9-4e37-9de0-93319273b8bc.

Another way is to use the Getting event of VersionControlServer.

1 Create a Visual Studio Add-in project ListenGetStatus

Add following references

EnvDTE

EnvDTE80

EnvDTE90

Microsoft.TeamFoundation

Microsoft.TeamFoundation.Client

Microsoft.TeamFoundation.VersionControl.Client

Microsoft.VisualStudio.TeamFoundation

Microsoft.VisualStudio.TeamFoundation .Client

Microsoft.VisualStudio.TeamFoundation.VersionControl

2 Implement OnStartupComplete

TeamFoundationServerExt tfsExt;
        VersionControlExt vsExt;
        public void OnStartupComplete(ref Array custom)
        {
            tfsExt = _applicationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt;
            if (tfsExt.ActiveProjectContext.DomainUri == null)
            {
                MessageBox.Show("Error");
                return;
            }


            vsExt = _applicationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt;

            vsExt.Explorer.Workspace.VersionControlServer.Getting += new GettingEventHandler(VersionControlServer_Getting);
            

        }


        static object locker = new object();
        void VersionControlServer_Getting(object sender, GettingEventArgs e)
        {
            lock (locker)
            {
                FileStream fs = null;
                StreamWriter writer=null;

                try
                {
                    fs = File.Open("C:\\GettingLog.txt", FileMode.Append);

                    writer = new StreamWriter(fs);
                    writer.WriteLine(string.Empty);
                    writer.WriteLine(DateTime.Now.ToString());
                    writer.WriteLine(String.Format("ServerItem={0}", e.ServerItem));
                    writer.WriteLine(String.Format("SourceLocalItem={0}", e.SourceLocalItem));
                    writer.WriteLine(String.Format("ChangeType={0}", e.ChangeType));
                    writer.WriteLine(String.Format("DeletionId={0}", e.DeletionId));
                    writer.WriteLine(String.Format("DiskUpdateAttempted={0}", e.DiskUpdateAttempted));
                    writer.WriteLine(String.Format("IsDelete={0}", e.IsDelete));
                    writer.WriteLine(String.Format("IsLatest={0}", e.IsLatest));
                    writer.WriteLine(String.Format("ItemId={0}", e.ItemId));
                    writer.WriteLine(String.Format("ItemType={0}", e.ItemType));

                    writer.WriteLine(String.Format("Status={0}", e.Status));
                    writer.WriteLine(String.Format("TargetLocalItem={0}", e.TargetLocalItem));
                    writer.WriteLine(String.Format("Version={0}", e.Version));
                    writer.WriteLine(String.Format("Workspace={0}", e.Workspace.DisplayName));

                }
                catch
                {

                }

                finally
                {
                    if (writer != null)
                    {
                        writer.Flush();
                        writer.Close();
                    }
                    if (fs != null)
                    {
                        fs.Flush();
                        fs.Close();
                    }
                }
                

            }

        }

3 In the above code

if (!e.IsDelete && e.IsLatest), so that the item is a new file and latest.

4 Copy the assembly under bin folder and ListenGetStatus.AddIn to My Documents\Visual Studio 2008\Addins

5 Notice that this needs TeamFoundationServer is available OnStartupComplete, you can use a Timer to check it timely if TeamFoundation is not available.

posted on 2010-03-05 11:11  Ruiz  阅读(744)  评论(0编辑  收藏  举报

导航