如何从C#代码将Visual Studio连接到Team Foundation Server Team Project

时间:2013-01-30 14:17:01

标签: c# tfs

是否可以从代码中将Visual Studio连接到TFS Team Project?我发现文章如何连接到TFS:http://msdn.microsoft.com/en-us/library/vstudio/bb286958.aspx但是这允许我从代码执行TFS的基本操作但不连接Visual Studio。 sample

2 个答案:

答案 0 :(得分:1)

据我了解你的问题,你不能......你有3个选择:

1 - 为VS Team Explore制作扩展程序,此扩展程序取决于您已使用VS连接。

Extending Team Explorer in Visual Studio 2012

2-制作一个单独的exe,这样你就可以使用 TeamProjectPicker 了     您可以选择要连接的TFS和哪个团队项目。

    using System.Windows.Forms;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Server;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application.EnableVisualStyles(); // Makes it look nicer from a console app.
                //"using" pattern is recommended as the picker needs to be disposed of
                using (TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.MultiProject, false))
                {
                    DialogResult result = tpp.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        System.Console.WriteLine("Selected Team Project Collection Uri: " + tpp.SelectedTeamProjectCollection.Uri);
                        System.Console.WriteLine("Selected Projects:");
                        foreach (ProjectInfo projectInfo in tpp.SelectedProjects)
                        {
                            System.Console.WriteLine(projectInfo.Name);
                        }
                    }
                }
            }
        }
    }

3-使用TFS和硬编码路径的API或将其放在如下的配置文件中:

using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;

namespace TfsApplication
{
    class Program
    {
        static void Main(String[] args)
        {
            // Connect to Team Foundation Server
            //     Server is the name of the server that is running the application tier for Team Foundation.
            //     Port is the port that Team Foundation uses. The default port is 8080.
            //     VDir is the virtual path to the Team Foundation application. The default path is tfs.
            Uri tfsUri = (args.Length < 1) ?
                new Uri("http://Server:Port/VDir") : new Uri(args[0]);

            TfsConfigurationServer configurationServer =
                TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

            // Get the catalog of team project collections
            ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
                new[] { CatalogResourceTypes.ProjectCollection },
                false, CatalogQueryOptions.None);

            // List the team project collections
            foreach (CatalogNode collectionNode in collectionNodes)
            {
                // Use the InstanceId property to get the team project collection
                Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
                TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);

                // Print the name of the team project collection
                Console.WriteLine("Collection: " + teamProjectCollection.Name);

                // Get a catalog of team projects for the collection
                ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren(
                    new[] { CatalogResourceTypes.TeamProject },
                    false, CatalogQueryOptions.None);

                // List the team projects in the collection
                foreach (CatalogNode projectNode in projectNodes)
                {
                    Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName);
                }
            }
        }
    }
}

答案 1 :(得分:0)

在这里,您可以找到将Visual Studio连接到TFS的分步指南

http://tfs.visualstudio.com/en-us/learn/code/connect-vs/