0

如何将最新版本的文件从 TFS 加载到计算机内存中?我不想从 TFS 获取最新版本到磁盘上,然后将文件从磁盘加载到内存中。

4

2 回答 2

2

能够使用这些方法解决:

VersionControlServer.GetItem 方法(字符串)
http://msdn.microsoft.com/en-us/library/bb138919.aspx

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

完整方法:

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);
            }  
        }
于 2013-03-12T04:27:01.423 回答
1

大多数时候,我想将内容作为(正确编码的)字符串获取,所以我采用了@morpheus 答案并对其进行了修改以执行此操作:

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);
    }
}
于 2016-08-04T12:32:57.067 回答