连接到 tfs 并下载其中存在的文件 VS2010

connect to tfs and download the files present in it VS2010

我想连接到 TFS 并下载其中的文件。我正在使用 VS2010 并尝试了以下代码。但是我好像哪里出错了:

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

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

C#代码:

static void Main(string[] args)
    {
        string teamProjectCollectionUrl = "https://YourTfsUrl/tfs/YourTeamProjectCollection";
        string filePath = "C:\project\myfile.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();
    }

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

尝试这样的事情...

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

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