简体   繁体   中英

TFS / File Checkout from C#

I don't have a great deal of experience with TFS, other than using it for source control. I am working on a C# application that will need to modify files that are being controlled by TFS. From within my C# application, how can I check out a file that is controlled via TFS?

Thanks - Randy

You can use PendEdit to make your files writables, make your changes to it, then you add it to the pending changes, and finally check it in.

Here is some code where a folder structure is created and then checked in (Very similar to what you will need).

    private static void CreateNodes(ItemCollection nodes)
{
    using (var tfs = TeamFoundationServerFactory.GetServer("http://tfsserver:8080"))
    {
        var versionControlServer = tfs.GetService(typeof (VersionControlServer)) as VersionControlServer;
        versionControlServer.NonFatalError += OnNonFatalError;

        // Create a new workspace for the currently authenticated user.             
        var workspace = versionControlServer.CreateWorkspace("Temporary Workspace", versionControlServer.AuthenticatedUser);

        try
        {
            // Check if a mapping already exists.
            var workingFolder = new WorkingFolder("$/testagile", @"c:\tempFolder");

            // Create the mapping (if it exists already, it just overides it, that is fine).
            workspace.CreateMapping(workingFolder);

            // Go through the folder structure defined and create it locally, then check in the changes.
            CreateFolderStructure(workspace, nodes, workingFolder.LocalItem);

            // Check in the changes made.
            workspace.CheckIn(workspace.GetPendingChanges(), "This is my comment");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            // Cleanup the workspace.
            workspace.Delete();

            // Remove the temp folder used.
            Directory.Delete("tempFolder", true);
        }
    }
}

private static void CreateFolderStructure(Workspace workspace, ItemCollection nodes, string initialPath)
{
    foreach (RadTreeViewItem node in nodes)
    {
        var newFolderPath = initialPath + @"\" + node.Header;
        Directory.CreateDirectory(newFolderPath);
        workspace.PendAdd(newFolderPath);
        if (node.HasItems)
        {
            CreateFolderStructure(workspace, node.Items, newFolderPath);
        }
    }
}

Using the other solution gave me permission problems.

Here's an alternative way to checkout your files using tf.exe :

//Checkout file
Process proc = new Process();
proc.StartInfo = 
    new ProcessStartInfo(
        @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe", 
        string.Format("checkout \"{0}\"", fileLocation)
    );
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();

For those looking to use the first solution and resolve the permission issue you can use the following code to use the current credentials, this replaces the "TeamFoundationServerFactory.GetServer" call then use the TfsTeamProjectCollection (tmPrjColl) to get the VersionControlServer:

using Microsoft.TeamFoundation.Client;
using MTVC = Microsoft.TeamFoundation.VersionControl.Client;
using MVSC = Microsoft.VisualStudio.Services.Common;

MVSC.VssCredentials creds = new MVSC.VssCredentials(new MVSC.WindowsCredential(true));
using (TfsTeamProjectCollection tmPrjColl = new TfsTeamProjectCollection(new Uri("<source control URL>"), creds))
{
    MTVC.VersionControlServer verCtrlSvr = tmPrjColl.GetService<MTVC.VersionControlServer>();
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM