Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move a TFS file with c# API?

Tags:

c#

api

tfs

I have been googling for a good time on how to move a file with c# using the TFS API. The idea is to have a folder on which the developers drop database upgrade scripts and the build process get's to the folder creates a build script and moves all the files on the folder to a new folder with the database build version that we just created.

I cannot seriously find any reference about moving files programatically in TFS... (aside of the cmd command line)

does anybody know of a good guide / msdn starting point for learning TFS source control files manipulation via c#?

like image 270
argatxa Avatar asked Sep 29 '08 14:09

argatxa


2 Answers

Its pretty simple :).

Microsoft.TeamFoundation.VersionControl.Client.Workspace workspace = GetMyTfsWorkspace();
workspace.PendRename( oldPath, newPath );

Then you need CheckIn it of course. Use a "workspace.GetPendingChanges()" and "workspace.CheckIn()" methods to do it.

like image 83
TcKs Avatar answered Oct 27 '22 12:10

TcKs


Here's a quick and dirty code sample that should get you most of the way there.

using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.VersionControl.Client; 


public void MoveFile( string tfsServer, string oldPath, string newPath )
{
    TeamFoundationServer server = TeamFoundationServerFactory.GetServer( tfsServer, new UICredentialsProvider() ); 
    server.EnsureAuthenticated(); 
    VersionControlServer vcserver = server.GetService( typeof( VersionControlServer ); 
    string currentUserName = server.AuthenticatedUserName;
    string currentComputerName = Environment.MachineName;
    Workspace[] wss = vcserver.QueryWorkspaces(null, currentUserName, currentComputerName);
    foreach (Workspace ws in wss)
    {

        foreach ( WorkingFolder wf in wfs )
        {
            bool bFound = false; 
            if ( wf.LocalItem != null )
            {
                if ( oldPath.StartsWith( wf.LocalItem ) )
                {
                   bFound = true; 
                   ws.PendRename( oldPath, newPath ); 
                   break; 
                }
             }
            if ( bFound )
               break; 
        }
    }
}
like image 32
Jason Diller Avatar answered Oct 27 '22 12:10

Jason Diller