How can I programmatically check-out an item for edit in TFS?

abatishchev picture abatishchev · Feb 23, 2011 · Viewed 35.7k times · Source

I'm working on a utility processing files being under source control using TFS 2010.

If an item is not yet checked-out for edit, I'm getting an exception, what is definitely predictable because file is in read-only mode.

What ways exist to check-out a file?

P.S. I want something for programmatic rather then Process.Start("tf.exe", "..."); if that's applicable.

Answer

Ben picture Ben · Feb 23, 2012

Some of the other approaches mentioned here only work for certain versions of TFS or make use of obsolete methods. If you are receiving a 404, the approach you are using is probably not compatible with your server version.

This approach works on 2005, 2008, and 2010. I don't use TFS any longer, so I haven't tested 2013.

var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
{
    var workspace = workspaceInfo.GetWorkspace(server);    
    workspace.PendEdit(fileName);
}