如何从TFS将最新版本的文件加载到计算机内存中?我不想从TFS到磁盘上获取最新版本,然后将文件从磁盘加载到内存中。
答案 0 :(得分: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);
}
}
答案 1 :(得分: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);
}
}