TFS :: Programmatically get folder content

In this small article, I would like to show you how to get the contents of the folder from TFS server.

The post could be useful while creating deployment tools.

1. Create a new Console Application

2. Add references to following assemblies

You can get those from the references assembly or from folder 
C:Program FilesMicrosoft Visual Studio 10.0Common7IDEReferenceAssembliesv2.0
3. Get the TFS server url 
Use Team Explorer to connect to a TFS server and use Properties window to the get the url.
4. Get the path to required folder
Get the folder path from Source Control Explorer
Eg: $/Projects/MyApplication
5. Use the following code to retrieve all folders
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace MyConsoleApp
{     class Program     {         static void Main(string[] args)         {             RegisteredProjectCollection server = RegisteredTfsConnections.GetProjectCollection(new Uri("URLHER"));             TfsTeamProjectCollection projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(server);             VersionControlServer versionControl = (VersionControlServer)projects.GetService(typeof(VersionControlServer));             Workspace workspace = versionControl.CreateWorkspace(AppDomain.CurrentDomain.FriendlyName + "-installer", versionControl.AuthorizedUser);             WorkingFolder folder = new WorkingFolder("$PATH", "c:\output");             workspace.CreateMapping(folder);             workspace.Get(); // Receive all the files             workspace.Delete();         }     }
6. Execute the application
Now you can see the c:output folder to view the files.
var dzone_url = ‘’;var dzone_title = ‘TFS :: Programmatically get folder content’;var dzone_blurb = ‘TFS :: Programmatically get folder content’;var dzone_style = ‘2’;

Leave a Reply

Your email address will not be published. Required fields are marked *