Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c# - 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: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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...