Richard R Richard R - 1 year ago 88
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 Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download