How to get a file from TFS directly into memory (i.e., don't want to read from file system into memory)?

跟風遠走 提交于 2020-01-25 07:12:10

问题


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.


回答1:


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);
            }  
        }



回答2:


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);
    }
}


来源:https://stackoverflow.com/questions/15346851/how-to-get-a-file-from-tfs-directly-into-memory-i-e-dont-want-to-read-from-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!