4

Is there any way to undo a checkout programmatically in C#?

The files get checked out programmatically, but if the code does not change on execution, I want the checkout to be undone.

public static void CheckOutFromTFS(string fileName)
{
    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
    if (workspaceInfo == null)
        return;

    var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
    var workspace = workspaceInfo.GetWorkspace(server);
    workspace.PendEdit(fileName);
}

The above code is my checkout code.

w.b
  • 11,026
  • 5
  • 30
  • 49
Matthijs
  • 3,162
  • 4
  • 25
  • 46

2 Answers2

7

You can use the Workspace.Undo method to undo the checkout.

http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.workspace.undo.aspx

Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
1

I've done this task in following way:

private const string ConstTfsServerUri = @"http://YourTfsAddress:8080/tfs/";
 #region Undo
    public async Task<bool> UndoPendingChangesAsync(string path)
    {
        return await Task.Run(() => UndoPendingChanges(path));
    }

    private bool UndoPendingChanges(string path)
    {
        using (var tfs = TeamFoundationServerFactory.GetServer(ConstTfsServerUri))
        {
            tfs.Authenticate();
            // Create a new workspace for the currently authenticated user.   
            int res = 0;
            try
            {
                var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(ConstDefaultFlowsTfsPath);
                var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
                Workspace workspace = workspaceInfo.GetWorkspace(server);
                res = workspace.Undo(path, RecursionType.Full);

            }
            catch (Exception ex)
            {
                UIHelper.Instance.RunOnUiThread(() => MessageBox.Show(ex.Message));
            }

            return res == 1;//undo has been done succesfully
        }
    }
Mr.B
  • 3,484
  • 2
  • 26
  • 40