0
votes

How can I load latest version of a file from TFS into computer memory? I do not want to get latest version from TFS onto disk, then load file from disk into memory.

2
From the command line? From the API?Edward Thomson

2 Answers

2
votes

was able to solve using these methods:

VersionControlServer.GetItem Method (String)
http://msdn.microsoft.com/en-us/library/bb138919.aspx

Item.DownloadFile Method
http://msdn.microsoft.com/en-us/library/ff734648.aspx

complete method:

private static byte[] GetFile(string tfsLocation, string fileLocation)
        {            
            // Get a reference to our Team Foundation Server.
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tfsLocation));

            // Get a reference to Version Control.
            VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

            // Listen for the Source Control events.
            versionControl.NonFatalError += OnNonFatalError;
            versionControl.Getting += OnGetting;
            versionControl.BeforeCheckinPendingChange += OnBeforeCheckinPendingChange;
            versionControl.NewPendingChange += OnNewPendingChange;           

            var item = versionControl.GetItem(fileLocation);
            using (var stm = item.DownloadFile())
            {
                return ReadFully(stm);
            }  
        }
1
votes

Most of the time, I want to get the contents as a (properly encoded) string, so I took @morpheus answer and modified it to do this:

private static string GetFile(VersionControlServer vc, string fileLocation)
{
    var item = vc.GetItem(fileLocation);
    var encoding = Encoding.GetEncoding(item.Encoding);
    using (var stream = item.DownloadFile())
    {
        int size = (int)item.ContentLength;
        var bytes = new byte[size];
        stream.Read(bytes, 0, size);
        return encoding.GetString(bytes);
    }
}