Using TF.exe and Codeplex

TF.exe is a Team Foundation command line tool that help you to run version control operations from a command prompt or script (bat or cmd). A detailed list of command can be found here.

I like Codeplex because I can keep my sources in sync. But because Codeplex is a free solution based of TFS it doesn’t offer all the facilities, like a build server. So, I started to automate a little bit this process and first thing was to do a get latest version from a console application.

1. First, a script file (cmd file) to get the sources from Codeplex

@ECHO OFF
SET PATH=%PATH%;c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\
SET getpath="D:"\TFSGet\Source

ECHO Trying TFS get...
ECHO.

REM IF NOT EXIST Create GetPath

IF NOT EXIST %Getpath% MD %Getpath%         

REM if exists remove WORKSPACE and create NEW WORKSPACE

tf workspace /login:snd\your_user,password /delete WORKSPACENAME /noprompt /s:tfs.codeplex.com\TFSXX

tf workspace /login:snd\your_user,password /new /s:tfs.codeplex.com\TFSXX WORKSPACENAME /noprompt

REM unmap default WORKING FOLDER for newly created WORKSPACE

tf workfold /login:snd\your_user,password /s:tfs.codeplex.com\TFSXX /workspace:WORKSPACENAME /unmap $/

REM map WORKING FOLDER for newly created WORKSPACE

tf workfold /login:snd\your_user,password /server:tfs.codeplex.com\TFSxx /workspace:WORKSPACENAME /map $/TFSPathToProject/ %Getpath%

REM get the recent path through DOS command
pushd %Getpath%
REM get latest source from TFS

tf get /login:snd\your_user,password /recursive /noprompt

ECHO.
REM move back to actual path through DOS command
popd
ECHO TFS get completed...pause

 

2. Call from C# code

It is a simple example of running a cmd file without showing the console and capturing the output and error messages in a variable.

    StringBuilder runInformation = new StringBuilder();
    string cmdFile = "Cmd\\getlatest.cmd";

    ProcessStartInfo getStartInfo = new ProcessStartInfo(cmdFile);
    getStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    getStartInfo.RedirectStandardError = true;
    getStartInfo.RedirectStandardOutput = true;
    getStartInfo.UseShellExecute = false;

    using (Process getProcess = Process.Start(getStartInfo))
    {

        getProcess.WaitForExit();

        string errorMessage = getProcess.StandardError.ReadToEnd();
        string outMessage = getProcess.StandardOutput.ReadToEnd();

        runInformation.AppendLine("Errors:");
        runInformation.AppendLine("---------------");

        if (!string.IsNullOrEmpty(errorMessage))
        {
            runInformation.AppendLine(errorMessage);
        }
        else
        {
            runInformation.AppendLine("No errors");
        }

        runInformation.AppendLine();
        runInformation.AppendLine();
        runInformation.AppendLine("Output:");
        runInformation.AppendLine("---------------");
        runInformation.AppendLine(outMessage);
    }

    return runInformation.ToString();
Advertisements