Tuesday, October 31, 2006

How to handle paths with TFS Version Control object model

After I have been developing with TFS Version Control object model for quite a while, I have come across very helpful class. I wish I did that couple of months before, as it would have saved me quite a bit of time in writing them string parsing functions.
I am talking about VersionControlPath class. This is a static class located in Microsoft.TeamFoundation.VersionControl.Common assembly, and it contains ton of routines you might need when working with version control items.

To give you a small sampling:


// Returns folder name from item path ("$/Project/Folder" from "$/Project/Folder/File.txt")


public static string GetFolderName(string item)


// Returns project name from item path ("Project" from "$/Project/Folder/File.txt")


public static string GetTeamProjectName(string item)


// Check whether specified path conforms to Windows or TFS path syntax (basically contains / or \ delimiter)


public static bool IsServerItem(string path)


// Prepends path with root $ char if required


public static string PrependRootIfNeeded(string folder)


// Parses item path and returns folder and file item paths


public static void Parse(string item, out string parent, out string name)




There is a dearth of other methods in VersionControlPath. Most of them are self-explanatory named, but may be a bit tricky to work with. For example, IsValidPath method will return true both for "$/Project/Folder" and "/Project/Folder" paths.

I wish I could direct you to MSDN, but documentation there is pretty thin (to be diplomatic about it).

If you find any interesting gotchas in that class, I would be delighted to know. Drop me a line.

1 comment:

Anonymous said...

It appears to be even more sparse than I would have expected. The comments should be more like the Workspace class (http://msdn2.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.workspace_members.aspx). Something must have gone wrong in generating the docs for the Microsoft.TeamFoundation.VersionControl.Common namespace.

I'll send some email to see if we can get it fixed.