Daryl
Daryl

Reputation: 18895

How to Use the TFS SDK to Determine If a Checked Out File Has Changed

I have a tool that generates C# files. I've been able to programmatically checkout the file using this method:

private static void CheckoutFile(string fileName)
{
    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
    var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
    var workspace = workspaceInfo.GetWorkspace(server);

    workspace.PendEdit(fileName);
}

But after the file has been generated I want to determine if there has been any changes to the file, and if not, to undo the check out. How do I do that with the TFS SDK?

Upvotes: 1

Views: 1052

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78883

Fundamentally, the TFS clients (tfpt uu, for example) determine whether you've changed a checked-out file by comparing the hash of the local file against the hash of the server item at the version that you have in your workspace.

To implement this behavior:

  1. Compute the MD5 hash of the local file
  2. Query the item on the server: workspace.VersionControlServer.GetItems(fileName, new WorkspaceVersionSpec(workspace), RecursionType.None)
  3. Compare the Item's HashValue with the value computed in step 1.

Upvotes: 2

Related Questions