连接和检查方法

时间:2013-11-13 08:57:34

标签: c# visual-studio-2010 tfs2010

我有一种方法可以连接到tfs并检出文件。我必须将它分成两种方法,因为它们不会连续发生。但是我不知道如何将它分成两种方法,因为如果我检查了,那就意味着我必须再次获得凭证和项目集合?

public static void Connect(String server, string path)
        {
            try
            {
                Uri serverUri = new Uri(server + "/tfs");
                ICredentialsProvider credentials = new UICredentialsProvider();
                TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
                tpc.EnsureAuthenticated();

                VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

                Workspace workspace = versionControl.TryGetWorkspace(path);
                workspace.PendEdit(path);

            }

2 个答案:

答案 0 :(得分:1)

我建议你不要让函数变为静态,然后你可以简单地将变量存储在类级别(如果它们是静态的,你仍然可以将它们存储在类级别,但至少这样你有一些范围作为到了生命周期:

public class TfsWrapper
{
    private TfsTeamProjectCollection tpc = null;
    private VersionControlServer versionControl = null;

    public TfsWrapper(string server, ...)
    {
        try
        {
            Uri serverUri = new Uri(server + "/tfs");
            ICredentialsProvider credentials = new UICredentialsProvider();
            tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri, credentials);
            tpc.EnsureAuthenticated();
            versionControl = tpc.GetService<VersionControlServer>();
        }
    }

    public void Checkout(string path)
    {
        Workspace workspace = versionControl.TryGetWorkspace(path);
        workspace.PendEdit(path);
    }

答案 1 :(得分:0)

我建议你使用这段代码,他会对封装和封装进行处理。许多服务器之间的重构方面和凭证

link:http://blogs.msdn.com/b/buckh/archive/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer.aspx