连接到 tfs 并下载其中的文件 VS2010 [英] connect to tfs and download the files present in it VS2010

查看:17
本文介绍了连接到 tfs 并下载其中的文件 VS2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接到 TFS 并下载其中的文件.我正在使用 VS2010 并尝试了以下代码.但似乎我在某处出错了:

I want to connect to TFS and download the files present in it. I am using VS2010 and tried the following code. But it seems I went wrong somewhere:

GetItem() 和 CopyTo() 方法的非静态字段方法需要对象引用"

"an object reference is required for the nonstatic field method" for GetItem() and CopyTo() methods

我的代码没有下载所有文件.

My code is not downloading all the files.

C# 代码:

static void Main(string[] args)
    {
        string teamProjectCollectionUrl = "https://YourTfsUrl/tfs/YourTeamProjectCollection";
        string filePath = "C:projectmyfile.cs";

        TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
        VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();

        Item item = versionControlServer.GetItem(filePath, VersionSpec.Latest);

        string fileString = string.Empty;

        using (Stream stream = item.DownloadFile())
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);


                using (StreamReader streamReader = new StreamReader(new MemoryStream(memoryStream.ToArray())))
                {
                    fileString = streamReader.ReadToEnd();
                }
            }
        }

        Console.WriteLine(fileString);
        Console.ReadLine();
    }

有人能帮我找到正确的方法吗?

Could anybody please help me out getting the proper approach?

推荐答案

试试这样...

    static void Main(string[] args)
    {
        string teamProjectCollectionUrl = "http://myserver:8080/tfs/DefaultCollection";
        string serverPath = "$/My Project/My SubFolder";
        string localPath = @"c:	empdownload";

        TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
        VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();

        foreach (Item item in versionControlServer.GetItems(serverPath, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.Any, true).Items)
        {
            string target = Path.Combine(localPath, item.ServerItem.Substring(2));

            if (item.ItemType == ItemType.Folder && !Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            else if (item.ItemType == ItemType.File)
            {
                item.DownloadFile(target);
            }
        }
    }

这篇关于连接到 tfs 并下载其中的文件 VS2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆