简体   繁体   中英

connect to tfs and download the files present in it 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:

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

My code is not downloading all the files.

C# Code:

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

Could anybody please help me out getting the proper approach?

Try something like this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM