gpt4 book ai didi

c# - 如何在没有物理文件路径的情况下将文件附加到 TFS 中的工作项?

转载 作者:行者123 更新时间:2023-11-30 16:58:02 24 4
gpt4 key购买 nike

我有一个 Microsoft.Office.Interop.Outlook.Attachment 类型的对象,我想将它附加到 TFS 工作项 (Microsoft.TeamFoundation.WorkItemTracking.Client.Microsoft. TeamFoundation.WorkItemTracking.Client)

不幸的是,我发现只有一种方法可以将附件添加到需要文件物理路径的 Microsoft.TeamFoundation.WorkItemTracking.Client.Microsoft.TeamFoundation.WorkItemTracking.Client。但就我而言,我在内存中有一个文件(Microsoft.Office.Interop.Outlook.Attachment 类型)。

如何将我的文件附加到 TFS 工作项?

注意:此代码不是我的问题的答案:

workItem.Attachments.Add(new Attachment("PATH OF MY ATTACHMENT", "COMMENT ABOUT ATTACHMENT"));

因为我没有任何路径。我也不想将我的文件保存到硬盘中,因为这种方式性能很差。

最佳答案

您可以使用 Microsoft.TeamFoundation.WorkItemTracking.Proxy.WorkItemServer 对象来实现(参见下面的代码)。与使用常规 TFS WIT 对象模型相比,它需要做更多的工作,但允许您将 Stream 对象用于附件,而不是使用物理文件路径。

using Microsoft.TeamFoundation.WorkItemTracking.Proxy;

var tpc = new TfsTeamProjectCollection(new Uri("<collection URL>"));
var store = tpc.GetService<WorkItemStore>();
var teamProject = store.Projects["<project name>"];
var server = tpc.GetService<WorkItemServer>();

FileAttachment attachment = new FileAttachment();
attachment.LocalFile = stream; /*this is the stream object with attachment contents*/
attachment.AreaNodeUri = teamProject.AreaRootNodes[0].Uri.ToString();
attachment.FileNameGUID = Guid.NewGuid(); /*just random guid*/
attachment.ProjectUri = teamProject.Uri.ToString();

server.UploadFile(attachment); /*upload the file to TFS*/

/*Time to attach the TFS file to the work item. We need to use Update() method directly*/
const string c_UpdatePackage =
@"<Package AttachmentUrl=""{7}/WorkItemTracking/v1.0/AttachFileHandler.ashx"" xmlns="""">
<UpdateWorkItem ObjectType=""WorkItem"" ClientCapabilities=""0"" WorkItemID = ""{0}"" Revision=""{1}"">
<InsertFile FieldName=""System.AttachedFiles"" OriginalName=""{2}"" FileName=""{3}"" CreationDate=""{4}"" LastWriteDate=""{4}"" FileSize=""{5}"" />
<Columns>
<Column Column=""System.ChangedBy"" Type=""String"">
<Value>{6}</Value>
</Column>
</Columns>
<ComputedColumns>
<ComputedColumn Column=""System.PersonId"" />
<ComputedColumn Column=""System.RevisedDate"" />
<ComputedColumn Column=""System.ChangedDate"" />
<ComputedColumn Column=""System.AuthorizedDate"" />
<ComputedColumn Column=""System.Watermark"" />
</ComputedColumns>
</UpdateWorkItem>
</Package>";

XmlDocument updatePackage = new XmlDocument();
updatePackage.LoadXml(string.Format(c_UpdatePackage,
1 /*work item ID*/,
2 /*work item latest revision*/,
"<file name you want, it will show up in the work item attachment tab>",
attachment.FileNameGUID,
DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"),
fileContent.Length,
"<display name of the TFS user making the change, e.g. John Smith>",
"<collection url, e.g. http://localhost:8080/tfs/defaultcollection>"));

XmlElement outputPackage; /*this can be ignored*/
string dbStamp; /*this can be ignored*/
IMetadataRowSets metadata; /*this can be ignored*/
server.Update(Guid.NewGuid().ToString(),
updatePackage.DocumentElement,
out outputPackage,
null,
out dbStamp,
out metadata);

关于c# - 如何在没有物理文件路径的情况下将文件附加到 TFS 中的工作项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com