Introduction
This tip helps you to track all changes made on a code base from a given date (change set) on TFS.
Background
There may be scenarios where a team of developers would want to track all the files they have added/modified from a date. My code snippet will help them to achieve this in a simple C# logic connecting to TFS.
Using the Code
Create a console application on Visual Studio.
You will have Program.cs by default. Now let's add TfsHelper.cs and put the below code into it:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
namespace Maintenance
{
public class TfsHelper
{
List<string> changedFiles = new List<string>();
public void Init(params string[] args)
{
string localPath = args[0];
string versionFromString = args[1];
TfsTeamProjectCollection tfs = null;
if (args.Length > 2)
{
tfs = new TfsTeamProjectCollection(new Uri(args[2]));
}
else return;
VersionControlServer vcs = tfs.GetService<versioncontrolserver>();
try
{
var changeSetItems = vcs.QueryHistory(localPath,
VersionSpec.ParseSingleSpec(
versionFromString,
null),
0, RecursionType.Full, null,
VersionSpec.ParseSingleSpec(
versionFromString,
null),
null, Int32.MaxValue, true, false);
foreach (Changeset item in changeSetItems)
{
DateTime checkInDate = item.CreationDate;
string user = item.Committer;
foreach (Change changedItem in item.Changes)
{
string filename = changedItem.Item.ServerItem.Substring
(changedItem.Item.ServerItem.LastIndexOf('/') + 1);
if (!filename.EndsWith(".dll")
&& !filename.EndsWith(".pdb")
&& !filename.EndsWith(".csproj")
&& !filename.EndsWith(".pubxml")
&& !filename.EndsWith(".sln")
&& !filename.EndsWith(".config")
&& !filename.EndsWith(".log")
&& filename.IndexOf(".") > -1
&& changedItem.ChangeType.Equals(ChangeType.Edit))
{
if (!Convert.ToBoolean(args[3]))
{
filename = string.Format("{0} - {1} - {2} by {3}",
filename,
checkInDate.ToString("dd-MM-yyyy"),
changedItem.ChangeType.ToString(),
user);
}
if (!changedFiles.Contains(filename))
{
if (Convert.ToBoolean(args[4]))
changedFiles.Add(changedItem.Item.ServerItem);
else
changedFiles.Add(filename);
}
}
}
}
foreach (string file in changedFiles)
Console.WriteLine(file);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
if (changedFiles != null && changedFiles.Count > 0)
Console.WriteLine
("-----------------------------------------\nTotal File count: " +
changedFiles.Count);
Console.WriteLine
("-----------------------------------------\nPress any key to close");
Console.ReadKey();
}
}
}
Now from your Program.cs, call your TfsHelper
as below:
class Program
{
static void Main(string[] args)
{
TfsChangedFiles();
}
private static void TfsChangedFiles()
{
<code>
<code>
<code>
<code>
<code>
TfsHelper TFS = new TfsHelper();
TFS.Init(@"C:\TfsWorkSpace", "1",
"https://mysite.visualstudio.com/DefaultCollection",
"true", "false")
}
}