1. 程式人生 > >使用C#從TFS獲取最新版本

使用C#從TFS獲取最新版本

http://www.cnblogs.com/chengxingliang/p/3163513.html

http://blogs.microsoft.co.il/shair/2014/09/10/tfs-api-part-55-source-control-get-history/

https://msdn.microsoft.com/zh-cn/library/bb286958.aspx

下載程式集:Microsoft.TeamFoundation.dll 、Microsoft.TeamFoundation.Client.dll、  Microsoft.TeamFoundation.Common.dll、Microsoft.TeamFoundation.VersionControl.Client.dll

 private static bool GetFilesFromTFS()
        {
            Uri tfsUri = new Uri(serverUrl);
            TfsTeamProjectCollection server = new TfsTeamProjectCollection(tfsUri);//登入TFS 

            VersionControlServer versionControl = (VersionControlServer)server.GetService(typeof(VersionControlServer));//獲取版本控制器服務

            //設定環境變數
            string UserName = versionControl.AuthenticatedUser;
            string HostName = System.Net.Dns.GetHostName();

            //建立工作區
            Workspace[] workspaces = versionControl.QueryWorkspaces(WorkSpaceName, UserName, HostName);//HostName

            Workspace workspace = null;
            if (workspaces.Length == 0)
            {
                try
                {
                    workspace = versionControl.CreateWorkspace(WorkSpaceName, UserName, "create workspace for manefist");
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                    return false;
                    //WorkingFolder folder = new WorkingFolder(ManifestTFSServerPath, ProjectLocalPath);
                    //versionControl.DeleteWorkspace(WorkSpaceName, UserName);
                    //WorkSpaceName = WorkSpaceName + "_" + Guid.NewGuid();
                    //Console.WriteLine("The workspace {0} {1} is exists in computer {2} ! it'll create a new workspace {3}", WorkSpaceName, UserName, HostName, WorkSpaceName);
                    //workspace = versionControl.CreateWorkspace(WorkSpaceName, UserName, "recreate workspace for manefist " + DateTime.UtcNow.ToString("yyyy-MM-dd mm:hh:ss"));
                    ////workspace.DeleteMapping(folder);
                }
            }
            else
            {
                workspace = workspaces[0];
            }

            if (!workspace.IsServerPathMapped(ManifestTFSServerPath))
            {
                if (!Directory.Exists(ProjectLocalPath))
                {
                    Directory.CreateDirectory(ProjectLocalPath);
                }
                try
                {
                    workspace.Map(ManifestTFSServerPath, ProjectLocalPath);
                }
                catch (Exception ex)
                {

                    Console.WriteLine("The mapping exists in {0}", ProjectLocalPath);
                    return false;
                }
            }
            TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
            //ProjectInfo projectInfo = tpp.SelectedProjects[0];

            ItemSpec spe = new ItemSpec(ManifestTFSServerPath, RecursionType.Full);
            ItemSet set = versionControl.GetItems(spe, VersionSpec.Latest, DeletedState.NonDeleted, ItemType.File, false);
            Item[] items = set.Items.Where(i => !i.ServerItem.ToLower().Equals(ManifestTFSServerPath.ToLower()) && i.ItemType == ItemType.File).ToArray();

            Console.WriteLine("Start Geting the latest version from tfs"+DateTime.Now.ToLongTimeString());
            foreach (Item item in items)
            {
                item.DownloadFile(workspace.GetLocalItemForServerItem(item.ServerItem));
                string itemName= item.ServerItem.Substring(item.ServerItem.LastIndexOf(@"/", System.StringComparison.Ordinal) + 1);
                Console.WriteLine(itemName+" finished!"+DateTime.Now.ToLongTimeString());
               
            }
            Console.WriteLine("Geting the latest version Successfully!" + DateTime.Now.ToLongTimeString());
            return true;
        }
    <!--TFS Start-->
    <add key="ProjectLocalPath" value="\\STCSRV-C81\MMFeedHealthyDatacache\manifest_BWInt"/>
      <!--\\LSSTCHOST06\mmfeeds\manifest_BWTest-->
    <add key="ManifestTFSServerPath" value="$/Bing/STC-A/Multimedia/Feed/manifest_BWInt"/>
    <add key="TFSServerUrl" value="http://vstfbing:8080/tfs/bing" />
    <add key="WorkSpaceName" value="ManefistWorkSpace003"/>
    <!--TFS End-->