【发布时间】:2013-02-27 02:49:08
【问题描述】:
如何将最新版本的文件从 TFS 加载到计算机内存中?我不想从 TFS 获取最新版本到磁盘,然后将文件从磁盘加载到内存中。
【问题讨论】:
-
从命令行?来自 API?
标签: tfs
如何将最新版本的文件从 TFS 加载到计算机内存中?我不想从 TFS 获取最新版本到磁盘,然后将文件从磁盘加载到内存中。
【问题讨论】:
标签: tfs
能够使用这些方法解决:
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);
}
}
【讨论】:
大多数时候,我想将内容作为(正确编码的)字符串获取,所以我接受了@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);
}
}
【讨论】: