Richard R Richard R - 22 days ago 16
C# Question

C# TFS get the last User that changed a specific File

Based on a specific file's location and name, I would like to use C# to programmatically get the name of the last User to have changed this file in TFS. (the last change date would be nice to have)

Though not directly related, as context information, Im using the following to get the latest from TFS based on a file's local path:

var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(LocalPath);
using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
{
var workspace = workspaceInfo.GetWorkspace(server);
GetRequest request = new GetRequest(new ItemSpec(LocalPath, RecursionType.Full), VersionSpec.Latest);
workspace.Get(request, GetOptions.Overwrite);
}


thank you for any hints

Answer

I created a simple code for your reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string tfsurl = "http://tfscollectionurl";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfsurl));
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            string[] path = { "$/Path/To/Item.cs" };
            ItemSpec[] itemSpecs = ItemSpec.FromStrings(path, RecursionType.Full);
            ItemSpec itemSpec = itemSpecs.Cast<ItemSpec>().First();
            IEnumerable<Changeset> changesets = vcs.QueryHistory(itemSpec);
            Changeset latestchangeset = changesets.Cast<Changeset>().First();
            Console.WriteLine(latestchangeset.Committer);
            Console.WriteLine(latestchangeset.CommitterDisplayName);
            Console.WriteLine(latestchangeset.Owner);
            Console.WriteLine(latestchangeset.OwnerDisplayName);
            Console.WriteLine(latestchangeset.CreationDate);
            Console.ReadLine();
        }
    }
}