TFS / 从C#中检出文件

6

我在TFS方面没有太多经验,只是用它进行源代码控制。我正在开发一个需要修改由TFS控制的文件的C#应用程序。从我的C#应用程序中,如何检出受TFS控制的文件呢?

谢谢 - Randy

3个回答

8

您可以使用PendEdit将文件设置为可写,对其进行更改,然后将其添加到待处理更改中,最后进行检入。

以下是创建文件夹结构并检入的代码(与您需要的非常相似)。

    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);
        }
    }
}

你不应该自己设置文件可写。Workspace.PendEdit()会为你完成这个操作。可写但未签出的文件代表一种不一致状态,在执行Get和Rename等操作时可能会导致不必要的冲突。 - Richard Berg
在这里,你实际上是如何传递凭据的? - M.R.

3

使用其他解决方案时,我遇到了权限问题。

以下是使用 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();

1
只是一个小提示,如果文件路径中有空格,则会出现问题:命令行应为“checkout "{0}"”。 - stefano m

0

对于那些想要使用第一种解决方案并解决权限问题的人,您可以使用以下代码来使用当前凭据,这将替换“TeamFoundationServerFactory.GetServer”调用,然后使用TfsTeamProjectCollection(tmPrjColl)来获取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>();
    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接