我可以像这样保存对WorkItem的附件:
TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);
FileInfo fi = new FileInfo(@"D:\\Docs\testfile.txt");
Attachment tfsAttachment = new Attachment(fi.FullName);
tfsWorkItem.Attachments.Add(tfsAttachment);
tfsWorkItem.Save();
但是当我尝试获取像这样的WorkItem的附件列表时:
TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);
foreach(Attachment tfsAttachment : tfsWorkItem.Attachments)
{
// Do things here
}
tfsWorkItem.Attachments
始终为空,即使我可以在TFS Web UI的“对象”选项卡中看到四个附件。我得到的WorkItem对象是正确的。
答案 0 :(得分:0)
我不记得所有细节,但the code I wrote a while ago使用包装类进行枚举,启用LINQ。
``` class AttachmentEnumerable:IEnumerable { private AttachmentCollection underlyingCollection;
public AttachmentEnumerable(AttachmentCollection coll)
{ underlyingCollection = coll; }
public IEnumerator<Attachment> GetEnumerator()
{
foreach (Attachment item in underlyingCollection)
{
yield return item;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
} ```
此外,Attachment
对象仅包含您稍后必须下载的URL。
答案 1 :(得分:-1)
我尝试了完全相同的场景,它对我有用。我可以附加文件,然后使用你提到的AttachmentCollection获取它。你可以尝试做的是在相同的上下文中执行它,如下所示,并验证在这种情况下AttachementCollection是否仍为空:
TfsTeamProjectCollection tfsTeamProjects = new TfsTeamProjectCollection(new Uri(tfsServerUrl));
WorkItemStore tfsWorkItemStore = tfsTeamProjects.GetService<WorkItemStore>();
WorkItem tfsWorkItem = tfsWorkItemStore.GetWorkItem(tfsWorkItemId);
FileInfo fi = new FileInfo(@"D:\\New Text Document.txt");
Attachment tfsAttachment = new Attachment(fi.FullName);
tfsWorkItem.Attachments.Add(tfsAttachment);
tfsWorkItem.Save();
foreach (Attachment tfsAttachment1 in tfsWorkItem.Attachments)
{
// Do things here
}
如果此处的集合不为空,那么第二次调用可能会出现另一个问题,即您尝试获取附件。