How to get a file from TFS directly into memory (i

2019-09-19 04:54发布

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.

标签: tfs
2条回答
家丑人穷心不美
2楼-- · 2019-09-19 05:38

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);
            }  
        }
查看更多
甜甜的少女心
3楼-- · 2019-09-19 05:51

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);
    }
}
查看更多
登录 后发表回答