【问题标题】:How to get a file from TFS directly into memory (i.e., don't want to read from file system into memory)?如何将文件从 TFS 直接读取到内存中(即,不想从文件系统读取到内存中)?
【发布时间】:2013-02-27 02:49:08
【问题描述】:

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

【问题讨论】:

  • 从命令行?来自 API?

标签: tfs


【解决方案1】:

能够使用这些方法解决:

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

【讨论】:

    【解决方案2】:

    大多数时候,我想将内容作为(正确编码的)字符串获取,所以我接受了@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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多