none
How to download the files from TFS to our local folder using c# application code? RRS feed

  • Question

  • Hi,

    I am a SSIS developer, but very new to C# coding. I have got a new requirement where I need to write a C# code to connect to TFS path and download the files from TFS to our local path or shared network. I am using the below code which I found from the link below but it is not working as expected, can anyone suggest me where am I going wrong?

    https://stackoverflow.com/questions/32453560/connect-to-tfs-and-download-the-files-present-in-it-vs2010

     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 above code is looping through the files what I have on the TFS, but it doing nothing for item.DownloadFile(target); Please help me out with this.

    Thanks!


    Rahul


    Tuesday, June 11, 2019 4:26 AM

All replies

  • Hi Rahul 11,

    Thank you for posting here.

    Since this thread is more related to TFS, I suggest that you could post in the following link.

    https://stackoverflow.com/questions/tagged/tfs

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 11, 2019 6:39 AM
  • I wouldn't say this post is more related to TFS, It is the complete C# code where we are just connecting to TFS. 

    Aparanjit

    Tuesday, June 11, 2019 1:57 PM
  • You'll want to use the REST API to download the files. Depends upon which VC you're using (VCS or Git) but the code is pretty straighforward. If you're using branching then this will impact things as well. 

    Here's a link to a Github repo that has code to read data from a TFS VC project with branching. `DownloadFolderAsync` is where the core code is to download a VC project's branch to the machine. You can start there.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 11, 2019 2:08 PM